$_ CLI In Depth
Learning progress 0%
lesson processes signals job-control

nohup, disown & SIGHUP

5 min read Module 10: Processes, Signals & Job Control Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:18

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.

BASH2 lines
nohup sleep 300 > job.log 2>&1 &
echo $! > job.pid

In 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.

Note

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.

BASH4 lines
sleep 300 &
jobs
disown %1
jobs

The 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.

BASH3 lines
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.

BASH2 lines
trap "" HUP
grep SigIgn /proc/$$/status

Because 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.

Warning

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.

BASH3 lines
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.

Signals, job control and hangups

Visualizer

A signal is a small numbered message the kernel delivers to a process. Some signals can be caught and some cannot. Job control and SIGHUP decide which signals a job gets.

1 / 5
Linux signal numbers (the same on this aarch64 machine)
SignalNo.Default actionCan a program catch it?
SIGHUP1terminateyes
SIGINT2terminateyes
SIGKILL9terminateno
SIGTERM15terminateyes
SIGCONT18continueyes
SIGSTOP19stopno
SIGTSTP20stopyes

The default action is what the kernel does when a program installs no handler. SIGKILL and SIGSTOP can never be caught or ignored.

sleep 30 & p=$!; kill -TERM $p; wait $p; echo "status $?"
Terminal lab · 2 steps · real Linux in your browser

Keep a job alive with nohup and a trap

Start a job with nohup, then start one whose shell ignores SIGHUP, and check the ignore bit.

01
Start a sleep with nohup
Run sleep 300 under nohup. Send its output to job.log so nohup does not create nohup.out, and save the PID of the background job in job.pid. ``bash nohup sleep 300 > job.log 2>&1 & echo $! > job.pid `` The SIGHUP bit in the SigIgn line of /proc/PID/status is set for this process.
Show hint for step 1
Redirect both stdout and stderr to job.log, add an ampersand, then echo $! into job.pid.
02
Ignore SIGHUP with a trap in a child shell
Start a bash child shell that sets an empty trap on HUP. The background sleep it starts inherits the ignored signal. Save that sleep PID in trap.pid. ``bash bash -c 'trap "" HUP; sleep 300 & echo $! > trap.pid' `` An empty trap command means ignore, and the ignore setting passes to the child.
Show hint for step 2
Use trap with empty double quotes and HUP, then start sleep with an ampersand and save $! into trap.pid.
Check yourself Question 1 of 4
Concept Check Signal sent on terminal close recall

Which signal does the kernel send to the session leader when the controlling terminal closes?

Recap & Key Takeaways

The kernel sends SIGHUP when the controlling terminal closes, and the shell forwards it to its jobs before exiting.
nohup runs a program with SIGHUP ignored, and it redirects output to nohup.out unless you redirect it yourself.
disown removes a job from the shell job table without changing the signal the job receives, so the job keeps its PID.
trap with an empty command ignores a signal, and the ignored state is inherited by programs the shell starts afterward.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue