nohup
Overview
nohup runs a command immune to SIGHUP, so it keeps running after you log out of a terminal session. It is a minimal “survive hangup” wrapper — not a full service manager. For interactive long-lived SSH work prefer tmux/screen; for production daemons prefer systemd (user or system units).
Syntax
nohup COMMAND [ARGS]...
nohup COMMAND [ARGS]... &Common Options
| Option | Description |
|---|---|
--help |
Usage |
--version |
Version |
nohup itself has almost no flags; behavior is about signals, redirection, and the shell background operator &.
Behavior (what you actually care about)
- Ignores
SIGHUPfor the started command. - If stdout is a terminal, output is appended to
./nohup.out(or$HOME/nohup.outif the cwd is not writable). - If you already redirect stdout/stderr,
nohupdoes not createnohup.out. - Exit status is that of
COMMAND, or 127 if the command cannot be invoked.
Key Use Cases
- Kick off a one-off long job before disconnecting SSH (when you did not start tmux)
- Quick background batch with a simple log file
- Legacy scripts that expect
nohupsemantics
Examples with Explanations
Example: basic background job
nohup ./long-job.sh &Starts the job, ignores hangup, backgrounds it. Output typically lands in nohup.out in the current directory.
Example: explicit log file
nohup ./long-job.sh > /var/tmp/long-job.log 2>&1 &
echo $!Always redirect in real use so logs do not scatter into surprise nohup.out files. $! is the background PID.
Example: find and stop later
nohup python3 train.py --epochs 100 > train.log 2>&1 &
echo $! > train.pid
# later:
kill -TERM "$(cat train.pid)"
# wait, then kill -KILL if neededRecord the PID; do not rely on grepping vaguely for python3.
Example: disown alternative (bash)
./long-job.sh > job.log 2>&1 &
disowndisown removes the job from the shell’s job table so logout does not send HUP to it. Different mechanism than nohup, similar operator goal.
Example: combine with nice / ionice
nohup nice -n 19 ionice -c3 ./batch-compress.sh > batch.log 2>&1 &Low CPU and idle I/O class — polite neighbor on shared hosts.
Example: remote one-liner over SSH
ssh host 'nohup /opt/app/rebuild.sh > /tmp/rebuild.log 2>&1 &'SSH can still race with job startup; for reliability prefer tmux new -d or a systemd unit.
Example: check nohup.out
nohup seq 1 1000000 &
tail -f nohup.outDemonstrates default output capture when no redirection is given.
Safety
nohupdoes not restart on crash, reboot, or OOM kill — not a supervisor.- Writing
nohup.outinto a shared or production cwd can fill disks or leak sensitive prints. - Background jobs may still receive other signals; only HUP is specially handled by
nohup. - Root + nohup + destructive scripts is still destructive after you disconnect.
Notes & Pitfalls
- Prefer
tmux new -s namefor anything you might need to reattach to interactively. - Prefer
systemd-run --useror a real unit for recurring or critical jobs. - Relative paths in the command resolve at start time; if you
cdlater in another shell, the job’s cwd is unchanged. nohup.outis append mode — old runs accumulate; rotate or redirect explicitly.- Some environments use
setsidorssh -fpatterns instead; know which layer owns the process.
Additional Resources
man nohupman systemd-run