December 5, 2026

nohup vs disown vs setsid: Three Ways to Keep a Process Alive After Logout

A half-closed laptop with a glowing terminal and a server still running behind it

A long job is running over SSH, the laptop lid is about to close, and the question is which of three commands will keep the job from dying with the terminal. nohup, disown and setsid all work. They work in three different ways, and the differences decide what happens to the job’s output. The tests below ran on Bash 5.2 and Ubuntu 24.04 with a pseudo-terminal that was hung up on purpose.

Video: gotbletu on YouTube

Why closing a terminal kills things

The hangup signal

When a terminal goes away, the shell attached to it receives SIGHUP, the hangup signal. The Bash manual describes the next step in two sentences: the shell exits by default on receiving SIGHUP, and “Before exiting, an interactive shell resends the SIGHUP to all jobs, running or stopped.” Stopped jobs are sent SIGCONT as well, so that they wake up long enough to receive it.

A process that has not arranged to handle SIGHUP dies when it arrives. That is the entire mechanism behind a job vanishing when the window closes.

Three places to break the chain

Each of the three commands interrupts that sequence at a different point:

  • nohup makes the process ignore the signal.
  • disown stops the shell from sending it.
  • setsid moves the process out of the terminal’s session so that it was never on the list.
Black corded telephone

The experiment

Five sleeps and one hangup

Five sleep commands were started from one interactive shell, each a different way, and then the terminal was closed from the outside.

$ sleep 1001 &
[1] 594
$ nohup sleep 1002 &
[2] 595
nohup: ignoring input and appending output to 'nohup.out'
$ sleep 1003 &
[3] 596
$ disown %3
$ setsid sleep 1004
$ sleep 1005 &
[3] 599
$ disown -h %+
$ jobs -l
[1]    594 Running                 sleep 1001 &
[2]-   595 Running                 nohup sleep 1002 &
[3]+   599 Running                 sleep 1005 &

Before the hangup, ps -eo pid,ppid,pgid,sid,tty,stat,args showed this. The shell is PID 592.

  PID  PPID  PGID   SID TT     STAT COMMAND
  594   592   594   592 pts/0  S    sleep 1001
  595   592   595   592 pts/0  S    sleep 1002
  596   592   596   592 pts/0  S    sleep 1003
  598     1   598   598 ?      Ss   sleep 1004
  599   592   599   592 pts/0  S    sleep 1005

And after:

  595     1   595   592 ?      S    sleep 1002
  596     1   596   592 ?      S    sleep 1003
  598     1   598   598 ?      Ss   sleep 1004
  599     1   599   592 ?      S    sleep 1005

The plain background job, PID 594, is gone. The other four survived. Each survivor’s parent is now PID 1, because the shell that started them has exited, and the terminal column reads ?.

nohup: ignore the signal, redirect the output

What it changes

nohup is a small wrapper program. The GNU coreutils manual says it runs the given command with hangup signals ignored, so the command can continue after the user logs out. The shell still sends SIGHUP. The process no longer cares.

It does a second thing that the other two do not: it deals with the terminal’s file descriptors. If standard output is a terminal, output is appended to a file called nohup.out in the current directory, or $HOME/nohup.out if that cannot be written. Standard error follows it to the same place. Standard input is redirected so the command cannot read from the terminal by mistake. The message in the transcript, “ignoring input and appending output to ‘nohup.out'”, is that happening.

The file it created in the test:

-rw------- 1 root root 0 Nov 30 21:16 nohup.out

Owner read and write only. The manual notes that files nohup creates get that mode regardless of the umask.

What it does not do

nohup does not put anything in the background. The manual is explicit that the & has to be added by the person typing. Without it the command runs in the foreground and holds the prompt. It also has to be chosen in advance, since it wraps the command at launch. A job that is already running cannot be wrapped after the fact.

Clipboard on a desk

disown: take the job off the shell’s list

A shell builtin, not a program

disown belongs to Bash. In its plain form it removes each job from the table of active jobs. The shell forwards SIGHUP to its jobs, and a process that is no longer listed as a job gets nothing. In the transcript, job 3 disappears from jobs -l after disown %3, and its number is reused by the next command.

With no argument, disown acts on the current job. -a covers all jobs and -r only running ones.

disown -h keeps the entry

disown -h is the gentler variant. The job stays in the table, still visible to jobs and still controllable with fg and bg, but it is marked so that SIGHUP is not sent to it when the shell receives one. PID 599 in the test was started that way and survived.

The advantage: it works after the fact

Because disown operates on a job that already exists, it rescues a command started without any planning. The usual sequence for a foreground job is Ctrl+Z to stop it, bg to resume it in the background, then disown.

The weakness: the terminal is still attached

disown changes nothing about the process itself. Its standard output still points at the terminal, and after logout that terminal no longer exists. A second test made the consequence visible. Three identical commands waited six seconds, by which time the terminal was gone, then ran echo hello and recorded the exit status of the echo:

Started with Exit status of echo Where “hello” went
cmd & then disown 1 Nowhere
nohup cmd & 0 nohup.out
setsid cmd 1 Nowhere

The two failures reported the same error:

bash: line 1: echo: write error: Input/output error

The processes lived. Their output did not, and each write failed. A program that checks its writes and exits on error would have stopped right there. The fix is to redirect at launch: cmd > job.log 2>&1 & followed by disown.

setsid: leave the session entirely

Sessions and controlling terminals

The ps listing has a column the other two methods never touched: SID, the session ID. Every process started from the shell carried the shell’s own PID, 592, as its session. The setsid process alone had a different one, 598, equal to its own PID, and ? in the terminal column before the hangup even happened.

The manual page for the command is one line long on this point: “setsid runs a program in a new session.” A new session has no controlling terminal, so there is no hangup to deliver. The process was never a job of the shell at all, which is why jobs -l never listed it.

It returned the prompt by itself

No & was typed after setsid sleep 1004, and the prompt came straight back. The man page explains why: the command calls fork if it is already a process group leader, and otherwise runs the program in the current process. A command launched from an interactive shell with job control is a group leader, so setsid forked, the child carried on in the new session, and the parent exited. The listing shows the result, a parent PID of 1 from the start.

In a script that behaviour is not guaranteed, and the -f flag forces the fork. The opposite flag, -w, makes setsid wait for the program and return its exit status.

Like disown, setsid leaves the file descriptors alone. The table above shows the same write error. Redirect the output.

Deck of a boat in the water

Side by side

nohup cmd & cmd & then disown setsid cmd
What it is External program Bash builtin External program
How it survives Ignores SIGHUP Shell does not send SIGHUP New session, no terminal
Works on a job already running No Yes No
Redirects output Yes, to nohup.out No No
Still listed by jobs Yes No (yes with -h) Never was
Session ID in the test Shell’s (592) Shell’s (592) Its own (598)

Choosing

For a command not yet started, nohup cmd > job.log 2>&1 & covers both the signal and the output in one line. For a command already running, disown is the only one of the three that can help, and it should have been started with redirection. setsid gives the cleanest separation and suits launching a daemon-like process from a script.

There is also a shell option that changes the picture. With huponexit set through shopt, the manual says Bash sends SIGHUP to all jobs when an interactive login shell exits, so on such a system even an ordinary exit takes background jobs down with it.

None of the three lets anyone reattach to the job later and see it running. That is a different tool’s job: a terminal multiplexer such as tmux or screen keeps the terminal itself alive on the server.

Sources

  • GNU Bash Reference Manual, “Signals”: https://www.gnu.org/software/bash/manual/html_node/Signals.html
  • GNU Bash Reference Manual, “Job Control Builtins” (disown): https://www.gnu.org/software/bash/manual/html_node/Job-Control-Builtins.html
  • GNU Coreutils manual, “nohup: Run a command immune to hangups”: https://www.gnu.org/software/coreutils/manual/html_node/nohup-invocation.html
  • setsid(1), Linux manual page (util-linux), man7.org: https://man7.org/linux/man-pages/man1/setsid.1.html

719 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *