What's the difference between nohup and ">& nohup.out &"?

5,322

Solution 1

In bash backgrounded jobs do not need to be protected by nohup

Depending on your version of bash, the behaviour can be changed. In the SIGNALS section of the manual page

If the huponexit shell option has been set with shopt, bash sends a SIGHUP to all jobs when an interactive login shell exits.

The huponexit option defaults to off

Solution 2

nohup allows you to disconnect the execution from the shell,

Nohuping backgrounded jobs is typically used to avoid terminating them when logging off from a remote SSH session. A different issue that often arises in this situation is that ssh is refusing to log off ("hangs"), since it refuses to lose any data from/to the background job(s).

This problem can also be overcome by redirecting all three I/O streams:
nohup ./myprogram > foo.out 2> foo.err < /dev/null &

Another reference to the SSH 'hangup' case.

Share:
5,322

Related videos on Youtube

Yang
Author by

Yang

Updated on September 17, 2022

Comments

  • Yang
    Yang over 1 year

    nohup renders a process immune to the shell's SIGHUP, but even if I run this from my shell:

    bash -c 'while true; do sleep 1; date; done' >& nohup.out &
    

    then log out and log back in, bash is still running and producing output to nohup.out. Is there any difference? Is relying solely on redirection less reliable in any way?

    • Peter Olsson
      Peter Olsson almost 14 years
      FYI: in general, these kinds of questions will probably be answered much faster on serverfault.
    • JJ_Australia
      JJ_Australia over 13 years
      @Mikeage: What, just because it's about bash?
  • Yang
    Yang almost 14 years
    This doesn't really answer my question, since redirection (without nohup) seems to be doing the same thing, as I described.