3

Consider the following scenario:

tail.sh:

#!/bin/bash
tail -f test.txt

invoke.sh:

#!/bin/bash
nohup ./tail.sh &

invoke_explicitredirect.sh:

#!/bin/bash
nohup ./tail.sh > out.log &

Running both in a terminal has the same effect:

  • I regain control of the terminal after running ./tail.sh
  • No output from tail appears on the terminal

However, when running it using ssh (e.g. ssh <user>@<hostname> "<script>"):

  • invoke_explicitredirect.sh returns control to ssh (and terminates)
  • invoke.sh hangs until I send a SIGINT

man nohup states that nohup will automatically redirect output to 'nohup.out' if possible:

If standard output is a terminal, append output to 'nohup.out' if possible, '$HOME/nohup.out' otherwise.

What is the difference between letting nohup append to nohup.out and explicitly redirecting the output?

3

Standard output is not a terminal when you launch the command remotely through ssh. Use the -t option with ssh to get the same behavior with both of your commands.

More details on ssh and nohup: http://snailbook.com/faq/background-jobs.auto.html (taken from the wiki page: https://en.wikipedia.org/wiki/Nohup)

  • I'm not sure what happened. All I get is Connection to <host> closed, and no nohup.out file. If I add echo test > out2.log to both scripts, out2.log gets created, but still no nohup.out – neverendingqs Dec 30 '14 at 20:24
  • Just tried it with sleep (ssh <user>@<hostname> "<script>; sleep 1") and it worked. Looks like ssh was terminating too early. – neverendingqs Dec 30 '14 at 20:25
  • What is standard output when ssh is used? – neverendingqs Dec 30 '14 at 20:28
  • The remote command standard output is going to the ssh client input in both cases but when -t is used, it behaves like a terminal. Nohup has a different output handling depending on whether it is a terminal or not. – jlliagre Dec 30 '14 at 21:42
  • More details on ssh and nohup: snailbook.com/faq/background-jobs.auto.html (taken from the wiki page: en.wikipedia.org/wiki/Nohup). – neverendingqs Dec 31 '14 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.