35

First this question is related but definitely not the same as this very nice question:

Difference between nohup, disown and &

I want to understand something: when I do '&', I'm forking right?

Is it ever useful to do "nohup ... &" or is simply & sufficient?

Could someone show a case where you'd be using '&' and still would want to use 'nohup'?

2
  • 1
    Have you read the accepted answer and its comment thread? They do explain what nohup does. What part has you confused? Commented Feb 24, 2012 at 18:49
  • 3
    @Gilles: it may be obvious to you, because you happen to be familiar with these things (seen your rep)... However, for a start, the accepted answer in the link I did put to does not say a word about using both nohup and '&'... I'm pretty sure my question is clear enough and different enough from the other, and two great answers here seems to prove that ; ) Also, are you really implying I did took the pain to link to another question, but without reading the accepted answer!? Commented Feb 28, 2012 at 16:12

4 Answers 4

40

First of all, every time you execute a command, you shell will fork a new process, regardless of whether you run it with & or not. & only means you're running it in the background.

Note this is not very accurate. Some commands, like cd are shell functions and will usually not fork a new process. type cmd will usually tell you whether cmd is an external command or a shell function. type type tells you that type itself is a shell function.

nohup is something different. It tells the new process to ignore SIGHUP. It is the signal sent by the kernel when the parent shell is closed.

To answer your question do the following:

  1. run emacs & (by default should run in a separate X window).
  2. on the parent shell, run exit.

You'll notice that the emacs window is killed, despite running in the background. This is the default behavior and nohup is used precisely to modify that.

Running a job in the background (with & or bg, I bet other shells have other syntaxes as well) is a shell feature, stemming from the ability of modern systems to multitask. Instead of forking a new shell instance for every program you want to launch, modern shells (bash, zsh, ksh, ...) will have the ability to manage a list of programs (or jobs). Only one of them at a time can be at the foreground, meaning it gets the shell focus. I wish someone could expand more on the differences between a process running in the foreground and one in the background (the main one being acess to stdin/stdout).

In any case, this does not affect the way the child process reacts to SIGHUP. nohup does.

6
  • 1
    +1 to both of you for the great answers... But I'm still confused... I tried that before answering my question: maybe my (very old) Debian Linux setup isn't correctly configured but if I "emacs &" and then type "exit", only my xterm exits: emacs stays there. Commented Feb 24, 2012 at 15:15
  • Emacs is complicated enough that it may handle SIGHUP on its own. It's only by default that processes exit on SIGHUP. A lot of 'daemon' programs like ntpd or inetd will re-read their configuration on SIGHUP instead of exiting. I'm a vim guy myself, so I don't have much experience with any emacs.
    – user732
    Commented Feb 24, 2012 at 16:58
  • 5
    There's nothing special about emacs. A shell typically sends SIGHUP to its child processes when the shell itself receives SIGHUP, not when the shell exits normally. bash has an option huponexit that causes it to send SIGHUP to children when it exits, but it's not enabled by default. gnu.org/software/bash/manual/bashref.html#Signals Commented Feb 24, 2012 at 19:28
  • 2
    hello. first of all, great explanation. I tried to verify your thesis by running a process with & and closed my shell by running exit. The process is still running. Any ideas on why it hasnt been killed? Or am I missing something here? Commented Jul 25, 2018 at 8:31
  • 2
    @rahmu I created a simple script in Python that creates a file every second for 60 seconds (dumb way to really see whether the script is running or not). I opened the terminal, run the script with & and then run the exit command. The terminal window has closed but the script has continued to create the files, ie. "was still running"... ?
    – Jeyekomon
    Commented Aug 6, 2018 at 10:31
21

Is it ever useful to do nohup ... &? Yes. If you just start a process "in the background" with &, that new process still has membership in the original shell's "process group". If that shell or the process group gets certain signals (SIGHUP, for example), by default they exit. This means that if you run a process with & from a shell started by an xterm, or rxvt or some other windowing terminal emulator, when you close the window, the background process gets a SIGHUP. Most casually-written code does not handle SIGHUP, and therefore exits.

If you do nohup ... &, the nohup command sets SIGHUP to ignored, and then execs the command. That newly exec'ed command keeps the signal mask that nohup setup, unless the command does some signal handling itself. If you close the xterm or rxvt or whatever, the kernel delivers SIGHUP to the command's process, which is ignored. It keeps running.

Doing a nohup on a command allows it to keep running after you close the xterm, or you log off.

2
  • Sorry - you lost me. Are you saying that using & with nohup keeps the command running in certain instances where only using nohup wouldn't? That seems to conflict with the answer here: unix.stackexchange.com/a/288064/1822
    – Mike B
    Commented Jun 7, 2016 at 15:21
  • 2
    @MikeB - The "&" does the usual fork/set things/exec system calls to put the command "in the background". That is, the command that gets nohupped is running asynchronously. The nohup does some things before the exec() system call to make the forked process ignore certain signals. So, yes, an "&" will allow a command to run under certain circumstances that a plain old nohup would not. Like closing the xterm associated with the shell that did the nohup, or logging off.
    – user732
    Commented Jun 7, 2016 at 16:46
0

One thing I don't see mentioned here, the "send HUP on terminal exit" event is configurable - it may or may not happen. This may be confusing some peoples' perception of how nohup is working. If sending HUP on terminal exit has been disabled, they will come to the conclusion nohup is not needed. Sending HUP on terminal exit is a shell option in bash.

Try

shopt | grep huponexit

This will tell you if HUP is even being sent on terminal exit. If it is, you will need to use nohup. If it is not, you won't.

2
  • bash will send a HUP signal to its jobs --even when huponexit is off, which is the default-- if bash itself is signaled with with a HUP (which happens when the terminal it runs in is torn down). See also this.
    – user313992
    Commented Feb 25, 2020 at 17:25
  • tl;dr; this answer is wrong: You DO need nohup even when huponexit is off, and nohup may no longer be enough (because of systemd and KillUserProcesses)
    – user313992
    Commented Feb 25, 2020 at 17:31
-1

if you run a program in the background ( with & as a suffix ) on linux operating system and logout even after that it will keep running: try out with:

  ping google.com > ping_result  &

After login back check the number of lines in the output file ping_result that will keep increasing means it is still running however it has been told that it will be closed. then what is the use of nohup command .

another scenario ==> as stated above for nohup emac & --> that should keep emac running after system logout but it doesn't show running after login back .

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .