Let’s check this small script
echo foo.1:`date` | tee $HOME/tmp/foo.txt
echo foo.2:`date` | tee -a $HOME/tmp/foo.txt
foo.1:Thu Nov 27 17:34:53 CET 2014
foo.2:Thu Nov 27 17:34:56 CET 2014
Very obvious, I write to the console, wait three seconds, then write to the console.
Ok, let’s take another script that would call this script in the background using &
bar.1:Thu Nov 27 17:36:32 CET 2014
bar.2:Thu Nov 27 17:36:32 CET 2014
foo.1:Thu Nov 27 17:36:32 CET 2014
foo.2:Thu Nov 27 17:36:35 CET 2014
bar is printing the date, calling
foo in the background, then printing the date, then it returns to you, and
foo is still running.
BUT this is only in a relative background …
Let’s try this
$ time $HOME/tmp/bar.sh > /dev/null
So it takes no time to run
bar you believe ?
Let’s try, for instance, over ssh (or cron or whatever)
$ time ssh localhost $HOME/tmp/bar.sh > /dev/null
bar suddenly waits 3 seconds for
foo to finish.
To be sure the script is sent to the farest background, you need to close the file descriptors, stdin, stdout, stderr
I rewrote it as
$HOME/tmp/foo.sh <&- >&- 2>&- &
$ time ssh localhost $HOME/tmp/baz.sh >/dev/null
Now the script
baz is immediately finished and does not wait for
foo to complete