What's the difference between nohup and ampersand
Asked Answered
E

7

320

Both nohup myprocess.out & or myprocess.out & set myprocess.out to run in the background. After I shutdown the terminal, the process is still running. What's the difference between them?

Erund answered 24/3, 2013 at 5:1 Comment(4)
what shell are you using? the behavior varies across shellsHarrow
bash. And I know why now according @nemo 's answer.Erund
@Erund if the answer suits your problem, please mark the question as accepted (the checkbox below the answer's votes) so it's not dangling around as unanswered. You should do so for all your questions :)Futurism
shutdown should be avoided as a term with a specific Linux meaning., and be replaced by exit.Yandell
F
403

nohup catches the hangup signal (see man 7 signal) while the ampersand doesn't (except the shell is confgured that way or doesn't send SIGHUP at all).

Normally, when running a command using & and exiting the shell afterwards, the shell will terminate the sub-command with the hangup signal (kill -SIGHUP <pid>). This can be prevented using nohup, as it catches the signal and ignores it so that it never reaches the actual application.

In case you're using bash, you can use the command shopt | grep hupon to find out whether your shell sends SIGHUP to its child processes or not. If it is off, processes won't be terminated, as it seems to be the case for you. More information on how bash terminates applications can be found here.

There are cases where nohup does not work, for example when the process you start reconnects the SIGHUP signal, as it is the case here.

Futurism answered 24/3, 2013 at 5:4 Comment(1)
It may be worth noting that just & does cause the subcommand to not receive some signals (e.g. SIGINT). nohup essentially adds SIGHUP to the list of signals that are not propagated.Etherege
A
77

myprocess.out & would run the process in background using a subshell. If the current shell is terminated (say by logout), all subshells are also terminated so the background process would also be terminated. The nohup command ignores the HUP signal and thus even if the current shell is terminated, the subshell and myprocess.out would continue to run in the background. Another difference is that & alone doesn't redirect the stdout/stderr so if there are any output or error, those are displayed on the terminal. nohup on the other hand redirect the stdout/stderr to nohup.out or $HOME/nohup.out.

Appendicular answered 24/3, 2013 at 5:12 Comment(3)
I run myprocess.out & and exit the shell. However, when I use ps aux | grep myprocess.out in other shell, I still can find "myprocess.out". It means than the process is still running, not be terminated.Erund
@Appendicular When killing the parent shell with kill -9 there won't be a SIGHUP as this would require the parent shell to handle SIGKILL, which it can't.Futurism
Check shopt | grep hupon as mentioned in the other answer.Appendicular
C
54

Most of the time we login to remote server using ssh. If you start a shell script and you logout then the process is killed. Nohup helps to continue running the script in background even after you log out from shell.

Nohup command name &
eg: nohup sh script.sh &

Nohup catches the HUP signals. Nohup doesn't put the job automatically in the background. We need to tell that explicitly using &

Cham answered 7/1, 2014 at 7:48 Comment(1)
Thanks. Wasn't expecting your answer but the fact that it is here is great. It answers the question I did not ask :DBarris
G
39

Using the ampersand (&) will run the command in a child process (child to the current bash session). However, when you exit the session, all child processes will be killed.

using nohup + ampersand (&) will do the same thing, except that when the session ends, the parent of the child process will be changed to "1" which is the "init" process, thus preserving the child from being killed.

Gottschalk answered 9/7, 2013 at 15:36 Comment(1)
as pointed out in @Devender's answer below, this is only true in a bash terminal if huponexit is set to 'on'Coverall
T
10

Correct me if I'm wrong

  nohup myprocess.out &

nohup catches the hangup signal, which mean it will send a process when terminal closed.

 myprocess.out &

Process can run but will stopped once the terminal is closed.

nohup myprocess.out

Process able to run even terminal closed, but you are able to stop the process by pressing ctrl + z in terminal. Crt +z not working if & is existing.

Tilla answered 7/2, 2017 at 4:51 Comment(1)
Technically ctrl Z just suspends the process - you can resume the process with bg (background) or fg (foreground). There might be some confusion with 'stop' as it could be interpreted as the process cannot be resumed.Arginine
G
6

The nohup command is a signal masking utility and catches the hangup signal. Where as ampersand doesn’t catch the hang up signals. The shell will terminate the sub command with the hang up signal when running a command using & and exiting the shell. This can be prevented by using nohup, as it catches the signal. Nohup command accept hang up signal which can be sent to a process by the kernel and block them. Nohup command is helpful in when a user wants to start long running application log out or close the window in which the process was initiated. Either of these actions normally prompts the kernel to hang up on the application, but a nohup wrapper will allow the process to continue. Using the ampersand will run the command in a child process and this child of the current bash session. When you exit the session, all of the child processes of that process will be killed. The ampersand relates to job control for the active shell. This is useful for running a process in a session in the background.

Groveman answered 20/12, 2015 at 18:10 Comment(0)
N
6

There are many cases when small differences between environments can bite you. This is one into which I have ran recently. What is the difference between these two commands?

1 ~ $ nohup myprocess.out &
2 ~ $ myprocess.out &

The answer is the same as usual - it depends.

nohup catches the hangup signal while the ampersand does not.

What is the hangup signal?

SIGHUP - hangup detected on controlling terminal or death of controlling process (value: 1).

Normally, when running a command using & and exiting the shell afterwards, the shell will terminate the sub-command with the hangup signal (like kill -SIGHUP $PID). This can be prevented using nohup, as it catches the signal and ignores it so that it never reaches the actual application.

Fine, but like in this case there are always ‘buts’. There is no difference between these launching methods when the shell is configured in a way where it does not send SIGHUP at all.

In case you are using bash, you can use the command specified below to find out whether your shell sends SIGHUP to its child processes or not:

~ $ shopt | grep hupon

And moreover - there are cases where nohup does not work. For example, when the process you start reconnects the NOHUP signal (it is done inside, on the application code level).

In the described case, lack of differences bit me when inside a custom service launching script there was a call to a second script which sets up and launches the proper application without a nohup command.

On one Linux environment everything worked smoothly, on a second one the application quit as soon as the second script exited (detecting that case, of course took me much more time then you might think :stuck_out_tongue:).

After adding nohup as a launching method to second script, application keeps running even if the scripts will exit and this behavior became consistent on both environments.

Noon answered 4/11, 2019 at 10:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.