Search code examples
clinuxposix-api

Will POSIX system(3) call to an asynchronous shell command return immediately?


For example, system("sh /mydir/some-script.sh &")


Solution

  • system("sh /mydir/some-script.sh &")
    

    executes

    /bin/sh -c 'sh /mydir/some-script.sh &'
    

    system will return as soon as outer shell returns, which will be immediately after it launches the inner shell. Neither shell will wait for some-script.sh to finish.

    $ cat some-script.sh
    sleep 1
    echo foo
    
    $ /bin/sh -c 'sh some-script.sh &' ; echo bar ; sleep 2
    bar
    foo