12

I'm trying to run a shell script with NSTask with the following code:

NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/Users/username/connect.sh"];
[task launch];

But I get An uncaught exception was raised and Couldn't posix_spawn: error 8

If I just run the script in terminal, everything works.

Here is what the script contains:

if [ ! -d ~/Remote/username/projects  ] 
then  
        sshfs -C -p 22 user@remotecomputer.com:/home/username ~/Remote/username        
fi
codenamepenryn
  • 451
  • 1
  • 7
  • 18
  • SourceTree hits the same issue running NSTasks: https://superuser.com/questions/1437788/sourcetree-custom-actions-couldnt-posix-spawn-error-8 – pkamb May 16 '19 at 20:25

2 Answers2

9

You can also add #!/bin/bash to the start of your script:

#!/bin/bash

if [ ! -d ~/Remote/username/projects  ] 
then  
    sshfs -C -p 22 user@remotecomputer.com:/home/username        ~/Remote/username        
fi
3

You need to use setLaunchPath like this:

[task setLaunchPath:@"/bin/sh"];

Then use setArguments for your script:

[task setArguments: [NSArray arrayWithObjects: @"~/connect.sh", nil]];
l'L'l
  • 44,951
  • 10
  • 95
  • 146