nohup

On this page 6

nohup is a standard Unix and Linux command that runs another program with the SIGHUP signal ignored, so the program keeps running when the controlling terminal closes or the user logs out. Among Linux processes, it is the simplest tool for keeping a long job alive beyond the session that started it.

The name stands for no hangup. The utility is specified by POSIX, ships in GNU coreutils and BusyBox, and works identically in every common shell. It does not place a process in the background, schedule it or supervise it; it changes one signal disposition and adjusts output redirection, then replaces itself with the target command.

How nohup protects a process from SIGHUP

When a terminal session ends, the kernel sends SIGHUP to the session’s foreground process group, and most shells forward it to their background jobs as well. The default action for SIGHUP is to terminate the process, which is why a long compile or data copy normally dies when an SSH connection drops.

nohup sets the disposition of SIGHUP to SIG_IGN and then calls exec to load the requested program in the same process. Because ignored signals remain ignored across exec, the new program inherits the protection and never sees the hangup. The process ID stays the same, so the job appears in ps under its own command name, not as nohup.

nohup also handles terminal-bound file descriptors, because writing to a closed terminal would otherwise fail or raise SIGPIPE. The table below summarizes what GNU nohup does with each standard stream.

StreamConditionAction taken by nohup
Standard outputConnected to a terminalAppended to nohup.out in the current directory
Standard outputCurrent directory not writableAppended to $HOME/nohup.out
Standard errorConnected to a terminalSent to the same place as standard output
Standard inputConnected to a terminalReplaced with an unreadable descriptor
Any streamAlready redirected by the userLeft unchanged
How nohup redirects standard streams

Syntax, exit codes and correct usage

The basic form is nohup command arguments &. The trailing ampersand is essential in interactive use: nohup itself does not detach, so without & the shell waits for the command and the terminal stays occupied. A typical production invocation also sets an explicit log file, as in nohup ./backup.sh > backup.log 2>&1 &.

nohup reports its own failures through reserved exit statuses, which scripts can test to tell a launcher problem from a failure of the job itself.

Exit statusMeaning
125nohup itself failed, for example it could not open nohup.out
126The command was found but could not be executed
127The command was not found
Any other valueThe exit status of the command that nohup ran
Exit statuses returned by nohup

Because nohup uses exec, it can only start an executable program, not a shell builtin, alias or function. To protect a pipeline or compound command, wrap it in a shell: nohup sh -c 'step1 && step2' > job.log 2>&1 &. The $! variable then holds the process ID for later monitoring or termination with kill.

nohup compared with disown, setsid and terminal multiplexers

Several mechanisms keep a process alive after logout, and they differ in when the decision is made and how much isolation they provide. nohup must be chosen before the command starts, while job control builtins can rescue a job already running.

MethodAppliedEffectOutput and reattachment
nohupBefore startIgnores SIGHUPFile only, no reattachment
disownAfter startRemoves the job from the shell job tableNo change, no reattachment
setsidBefore startCreates a new session without a terminalMust be redirected manually
screen or tmuxBefore startKeeps a virtual terminal aliveFull reattachment to live output
systemd-run or a unitBefore startRuns under the service managerJournal logging, restart policies
Ways to keep a Linux process running after the session ends

The bash builtin disown -h marks a job so the shell does not forward SIGHUP, which makes it the closest after-the-fact equivalent of nohup. setsid offers stronger detachment because the process has no controlling terminal at all, so no terminal hangup can ever reach it.

Limitations and common mistakes with nohup

nohup protects against exactly one signal. A process started with it still dies on SIGTERM, SIGKILL, an OOM killer decision or a reboot, and it is never restarted. For anything that must stay up reliably, a systemd service or another supervisor is the appropriate tool.

On many modern distributions, systemd-logind can be configured with KillUserProcesses=yes, which terminates every process in the user’s session scope at logout regardless of signal dispositions. In that configuration, nohup alone is not sufficient, and the job should be started with systemd-run --user together with lingering enabled, or as a proper unit.

The table below lists frequent symptoms, their causes and the usual fix.

SymptomCauseFix
Terminal stays blockedMissing & after the commandAdd & or use Ctrl+Z and bg
Disk fills with nohup.outUnbounded output appended to one fileRedirect to a rotated log or /dev/null
Job still dies at logoutlogind kills the session scopeUse systemd-run --user or a service
Job stops when reading inputProgram expects terminal inputSupply input from a file or redirect stdin
Program exits on hangup anywayProgram installs its own SIGHUP handlerUse setsid or a supervisor
Typical nohup problems and remedies
  • Programs may override the setting: an application that installs a SIGHUP handler, often to reload configuration, will act on hangups despite nohup.
  • Relative paths in nohup.out: the file lands in the working directory at launch time, which surprises users who start jobs from varying directories.
  • No priority change: nohup does not alter scheduling, so combine it with nice when a background job should yield CPU time.

Explained without jargon

When you close a terminal window or lose a remote connection, Linux tells every program started from it that the line has been hung up, and most programs simply quit. nohup is a small helper that tells a program to ignore that message.

It works like asking a colleague to keep working even after you leave the office. The work continues, and anything the program would have printed on screen is saved to a file instead, so it can be read later. It does not watch over the program or restart it if something else goes wrong.

A typical use case

A database administrator connects over SSH from a hotel network to export a 200 GB table from a Linux server. The export is expected to take about three hours, and the connection has dropped twice already that evening. Instead of running the dump directly, the administrator types nohup pg_dump -Fc sales > /backup/sales.dump 2> /backup/sales.err & and notes the process ID printed by the shell.

Forty minutes later the SSH session drops again. After reconnecting, the administrator checks the process with ps -p followed by the ID and watches the dump file grow with ls -lh. The export finishes unattended shortly before midnight, and the error file stays empty, confirming a clean result. For the next nightly export, the team moves the same command into a scheduled systemd timer so it no longer depends on any interactive session.