Is it possible in Bash to call a shell script from another shell script but not have the original script wait for the sub-script to complete?
2 Answers
Just fork it with a &
. As in, sh /path/to/script/script.sh &
This will print messages from the subscript, but you can replace the & with >/dev/null &
and suppress the output.
-
So the original script will continue on and can complete with a 0 exit code regardless of if the called script is still running, correct? What if set -e is invoked, does it make a difference?– user160910Jun 18, 2013 at 14:59
-
It doesn't. Once it's forked, it's on its own process and PID.
nohup
as mentioned by the other answer will also keep the process going through logouts and such.– Nathan CJun 18, 2013 at 16:12 -
right after you fork the process, you can "capture" its PID with $!. E.g.
mypid=$!
. I recommend then usingwait $mypid
at some point in your script to ensure that the forked process ends before ending the script. Jun 19, 2013 at 3:29
You should use "nohup" to make sure the process / script completes even if your user is logged out:
nohup /my/script.sh &