Problem
Nohup myprocess.out & or myprocess.out & configure myprocess.out to run in the background are both valid options. The process is still running after I shut down the terminal. What’s the distinction between the two?
Asked by Yarkee
Solution #1
The hangup signal is caught by nohup (see man 7 signal), but not by the ampersand (unless the shell is configured that way or doesn’t send SIGHUP at all).
When you run a command with & and then quit the shell, the shell will normally terminate the subcommand with the hangup signal (kill -SIGHUP pid>). This can be avoided by using nohup, which captures and ignores the signal, ensuring that it never reaches the actual program.
If you’re using bash, you can check whether your shell delivers SIGHUP to its child processes with the command shopt | grep hupon. Processes will not be terminated if it is turned off, as it appears to be the case for you. Here’s where you can learn more about how bash terminates programs.
There are times when nohup fails, such as when the process you’re running reconnects the SIGHUP signal, which is the case here.
Answered by nemo
Solution #2
myprocess.out & would use a subshell to run the process in the background. If the current shell is terminated (for example, by logging out), all subshells are terminated as well, and the background process is terminated as well. The nohup command ignores the HUP signal, therefore the subshell and myprocess.out will continue to execute in the background even if the current shell is stopped. Another distinction is that & by itself does not redirect stdout/stderr, therefore any output or errors are displayed on the terminal. stdout/stderr are redirected to nohup.out or $HOME/nohup.out in nohup.out.
Answered by amit_g
Solution #3
We use ssh to connect to external servers the majority of the time. The process is destroyed if you run a shell script and subsequently logout. Nohup allows you to keep the script running in the background even after you log out of the shell.
Nohup command name &
eg: nohup sh script.sh &
The HUP signals are caught by Nohup. Nohup does not automatically put the job in the background. That must be stated explicitly with the use of &.
Answered by Neethu George
Solution #4
The ampersand (&) causes the command to run in a child process (child to the current bash session). When you depart the session, however, all child processes will be terminated.
Using nohup + ampersand (&) achieves the same result, with the exception that when the session ends, the child process’s parent is changed to “1,” which is the “init” process, preventing the child from being destroyed.
Answered by Michel
Solution #5
If I’m wrong, please correct me.
nohup myprocess.out &
When the terminal is closed, nohup detects the hangup signal and sends a process.
myprocess.out &
The process can continue to operate, but it will be terminated once the terminal is closed.
nohup myprocess.out
The process can run even if the terminal is closed, however you can stop it by hitting ctrl + z in the terminal. If & is present, Crt +z will not operate.
Answered by John Joe
Post is based on https://stackoverflow.com/questions/15595374/whats-the-difference-between-nohup-and-ampersand