The ampersand &
will put the process into the background. File descriptors (stdout, stderr, stdin) are unaffected.
On shell exit, the shell will send SIGHUP
to everything it spawned, so when this shell closes, the background process will receive SIGHUP
and probably terminate. In some shells (bash, zsh) you can prevent this by using the command disown
which will tell the shell to not send it SIGHUP
on shell closing, and the process will continue to run.
You can also perform the equivalent of 'backgrounding using an ampersand' by sending SIGSTOP
(usually Ctrl-Z in your terminal) and then backgrounding the process (bg
).
nohup works by ignoring SIGHUP. This signal disposition is then passed to the child process you run. nohup also checks to see where stdout is going, and if its going to the terminal, it redirects stdout and stderr to a file. After the signal disposition change, and the possible redirect, it runs your process in the background.
So, whats the difference?
A background process doesn't ignore SIGHUP
, though you can tell your shell not to send it with disown. Unless prevented, nohup
redirects stdout to nohup.out files, which I tend to hate, if only that I need to delete nohup.out files randomly scattered on my disk.
There is also setsid
, which makes your process a process group leader. A process group leader will never get SIGHUP
sent to it by any shell. setsid
does not much with stdout/stderr, and I tend to use setsid
instead of nohup
.