2

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 Answers2

6

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.

Nathan C
  • 14,901
  • 4
  • 42
  • 62
  • 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? –  Jun 18 '13 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 C Jun 18 '13 at 16:12
  • right after you fork the process, you can "capture" its PID with $!. E.g. `mypid=$!` . I recommend then using `wait $mypid` at some point in your script to ensure that the forked process ends before ending the script. – senorsmile Jun 19 '13 at 03:29
1

You should use "nohup" to make sure the process / script completes even if your user is logged out:

nohup /my/script.sh &
Pascal Schmiel
  • 1,728
  • 12
  • 17