Running shell script with NSTask causes posix_spawn error

codenamepenryn picture codenamepenryn · Aug 29, 2014 · Viewed 8.6k times · Source

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

Answer

planetmik picture planetmik · Feb 1, 2016

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