Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

nohup Command Guide for Running Programs That Survive Logout

nohup runs a command that keeps going even when you close the terminal or log out.


What is nohup, and Why Use It?

The nohup command (“no hangup”) runs a program immune to the SIGHUP signal — the signal the kernel sends to a session’s processes when the terminal goes away. Without it, closing the SSH window kills your long-running job; with nohup, the program keeps running. Its output goes to a file called nohup.out by default, since there is no longer a terminal to display it on.

Why Learn nohup?

  • SSH sessions die: every disconnect sends SIGHUP; nohup is the cheapest shield against it.
  • Zero setup: one word in front of the command — no config files, no systemd units.
  • Great for one-offs: a 6-hour compile or a big download you cannot afford to restart.
  • Builds the habit: nohup teaches detachment; systemd units are the permanent version of the same idea.

nohup is the difference between “my job dies when my laptop closes” and “my job finishes somewhere, quietly, and waits for me to come back.”


1. Basic Usage: Start a Durable Job

# Run a long job immune to hangups
nohup ./long-job.sh &

# Output lands in nohup.out in the current directory
cat nohup.out

The & puts it in the background of this shell; nohup guarantees it survives the session. You need both — nohup alone still blocks the terminal.


2. Controlling the Output

# Redirect stdout to a named file
nohup ./job.sh > job.log 2>&1 &

# Redirect both streams, append mode
nohup ./job.sh >> job.log 2>&1 &

# Tail it as it grows
tail -f job.log

By default nohup writes to nohup.out, appending if the file already exists. Redirecting explicitly is clearer in scripts and avoids surprise mixing with older runs.


3. Checking It Is Still Alive

# Is the process running?
pgrep -fl job.sh

# Or look for it in the full list
ps aux | grep job.sh

# Did it finish? Check the output file
less nohup.out

Because the job is detached from job control, jobs will not show it after the shell exits — use ps or pgrep instead.


4. When to Use nohup vs. Something Else

ToolBest For
& aloneShort jobs in a session that will stay open
bg + disownJobs already running that you want to keep
nohupOne-off long jobs over SSH
systemd unitsAnything permanent, scheduled, or auto-restarting
# Already started a job and now regretting it? disown saves it too:
./long-job.sh &
disown -h %1        # remove from shell's job table; SIGHUP-proof in most shells

5. Cleaning Up

# Find the process
pgrep -fl job.sh

# Kill it politely, then forcefully if needed
kill 1234
kill -9 1234

nohup protects against hangups, not against you — remember to stop jobs you no longer want, and delete the growing nohup.out files you have accumulated.


Common Pitfalls

  • Forgetting the &: nohup job.sh without & still occupies the terminal; only the signal immunity is gained.
  • Assuming output is visible: with no terminal, everything goes to nohup.out — redirect explicitly if you want it elsewhere.
  • nohup does not survive reboots: it survives logout, not power loss. For that, use a systemd unit.
  • Losing track of the PID: write it down (echo $! > job.pid) or find it later with pgrep.

PageWhy It Matters
jobsThe job-control view that nohup jobs leave behind.
bgBackgrounding without immunity — the step before detachment.
psConfirm the detached job is still running.
pgrepFind the PID of a nohup’d job later.
systemd unitsThe production-grade way to keep programs running.

nohup Cheatsheet

CommandWhat It Does
nohup ./job.sh &Run a job that survives logout.
nohup ./job.sh > job.log 2>&1 &Same, with output to a known file.
tail -f nohup.outWatch the default output file.
pgrep -fl job.shFind the running job.
kill PIDStop it.
disown -h %1Make a shell job hangup-proof instead.