Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running shell script with NSTask causes posix_spawn error

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 [email protected]:/home/username ~/Remote/username        
fi
like image 583
codenamepenryn Avatar asked Aug 28 '14 23:08

codenamepenryn


2 Answers

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

#!/bin/bash

if [ ! -d ~/Remote/username/projects  ] 
then  
    sshfs -C -p 22 [email protected]:/home/username        ~/Remote/username        
fi
like image 114
planetmik Avatar answered Oct 16 '22 15:10

planetmik


You need to use setLaunchPath like this:

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

Then use setArguments for your script:

[task setArguments: [NSArray arrayWithObjects: @"~/connect.sh", nil]];
like image 42
l'L'l Avatar answered Oct 16 '22 15:10

l'L'l