Consider script s2:
#!/bin/bash
ssh localhost tail
And script s1
#!/bin/bash
./s2 &
sleep 1000
wait
Now call s1. It won't call the ssh command.
If you remove the & in s1 it will. If you call ./s2 directly it will. If you type in the console ./s2 & it will.
But ./s1 won't work. Why ?
Sure it calls the
ssh
command. It has to! There's no magic.It's a big mistake to assume that just because the script exits immediately when you remove
sleep
, it is not callingssh
. That conclusion is incorrect. There can be other reasons to explain this observed behavior.If you wanted to verify that
ssh
did not run, one way to do that would be to look at your system logs.The script exits very soon (not immediately) because
ssh
exits very soon. Whens2
runs, it receives emptystdin
, which thetail
command consumes and outputs nothing, and so thessh
shell terminates without any output. That's all.Change
s2
to this to see the difference:This way
s2
will have a non-emptystdin
to work with, and should produce some output.Btw the
sleep 1000
was unnecessary from the beginning, thanks to thewait
.