Where SIGHUP comes from
SIGHUP is signal 1. The kernel sends it in three main situations: the controlling terminal has been closed, the session leader has exited, or a terminal driver detects a hangup on a modem line. For a terminal window, the first case is the one you meet. The emulator closes its master side, the kernel sees that the slave has no more readers, and it delivers SIGHUP to the foreground process group and to the session leader.
The default action of SIGHUP is to terminate the process. A shell that receives it normally exits, and before it exits it resends SIGHUP to the jobs it still controls. A job started with an ampersand is one of those jobs, so closing the window usually kills it along with the shell. The fix is not to stop the signal from being sent. You change what the job does when the signal arrives, or you remove the job from the shell before the shell has a chance to forward the signal.
nohup: ignore the hangup
The nohup program runs another program with SIGHUP set to ignored. Because ignored signals stay ignored across exec, the child program never sees the hangup. The program also gets a few other changes. If standard input is a terminal, nohup redirects it from /dev/null. If standard output is a terminal, it writes the output to a file named nohup.out in the current directory, unless you redirect the output yourself.
nohup sleep 300 > job.log 2>&1 &
echo $! > job.pidIn this form the output goes to job.log, so nohup does not create nohup.out. The error output is also redirected, because a message from nohup itself would otherwise appear on the terminal. The $! variable records the PID of the background process, which is the nohup process. Because nohup executes the sleep, the PID is the same as the sleep PID.
nohup only protects against SIGHUP. It does not protect against SIGKILL, SIGTERM from a system shutdown, or an out-of-memory kill. Use it for one job that should outlive a closed terminal, not as a general service manager.
disown: remove the job from the table
The disown builtin is a bash feature. It removes a job from the shell job table, so the shell no longer forwards SIGHUP to it when the shell exits. The job itself is not changed, and it keeps running with the same PID. You can name the job with a job spec, or leave the argument out to use the current job.
sleep 300 &
jobs
disown %1
jobsThe first jobs shows the sleep. After disown, the second jobs shows nothing, because the shell no longer tracks that job. You cannot use fg or wait on it afterward, so you must have recorded its PID first. The process still exists, and you can check it with ps.
Because disown does not change the signal disposition, a disowned job still dies if it receives SIGHUP from a terminal that it is attached to. Some people combine the two tools, using nohup to ignore the hangup and disown to keep the shell from tracking the job. Either tool alone is often enough.
Ignoring SIGHUP from inside a script
A script can ignore SIGHUP for itself with trap. Setting the trap to an empty string ignores the signal, and the ignored state is inherited by the programs the script starts. This is the same effect as nohup, applied to the whole script rather than one command.
trap "" HUP
sleep 300 &
echo "ignoring HUP, pid $!"The first line sets SIGHUP to ignored for the shell. The background sleep inherits that disposition, so a hangup does not reach it. The message reports the PID so you can check the process later. A trap with an empty command ignores the signal, whereas a trap with a command runs that command when the signal arrives.
Checking whether a process ignores HUP
The kernel records the disposition of each signal in the SigIgn line of /proc/PID/status. The field is a bit mask with one bit per signal, written in hexadecimal. Bit 1, the value for SIGHUP, is the lowest bit. A value with that bit set means the signal is ignored.
trap "" HUP
grep SigIgn /proc/$$/statusBecause the shell itself has set SIGHUP to ignored, the mask it prints includes that bit. You can compare it with the mask of a fresh shell, which does not ignore the signal. The mask is a hexadecimal number, so read it as a pattern of bits rather than as a decimal count.
Ignoring SIGHUP in a shell affects every program you start from that shell after the trap is set. Do not use a global ignore in an interactive shell unless you intend it, because it can hide the fact that a terminal has closed.
Hands-on: keep a job alive
Start a job with nohup, record its PID, and confirm that it runs with the expected command. The check in the lab looks only at the file, so you can run these commands in the terminal and see the process in ps.
nohup sleep 300 > job.log 2>&1 &
echo $! > job.pid
ps -o pid,args -p "$(cat job.pid)"The ps line shows the sleep command with the PID you recorded. Because the output was redirected, job.log stays empty, and no nohup.out file appears in the directory. The PID is the value that you would use later with kill to stop the job.