nohup, disown, and screen: Keeping Processes Running After Logout
What You'll Learn
- Why processes die when you close a terminal or SSH session
- How to use
nohup,disown, andscreento keep processes alive after logout - When to reach for each tool
Quick Reference
| Situation | Tool |
|---|---|
| Start a command that survives logout | nohup cmd & |
| Detach an already-running job | disown %1 |
| Keep a full session and reattach later | screen or tmux |
Why Do Processes Die When You Log Out?
When you close a terminal or drop an SSH connection, the shell sends SIGHUP (Signal 1: Hangup) to every process in its job table. The default signal handler exits, so background jobs die along with the shell.
$ sleep 300 & [1] 12345 $ exit # shell exits → SIGHUP sent to sleep → PID 12345 terminates
The three tools below each cut the SIGHUP delivery path at a different layer.
nohup — Use Before Starting
nohup ("no hangup") starts a command with SIGHUP ignored from the very beginning.
$ nohup long-job.sh &
&is optional but idiomatic — background execution is the usual intent- STDOUT and STDERR are automatically redirected to
nohup.outin the current directory - Specify a custom log file to avoid
nohup.outgrowing unbounded:
$ nohup ./deploy.sh > /var/log/deploy.log 2>&1 &
Discard output entirely when you don't need it:
$ nohup ./job.sh > /dev/null 2>&1 &
nohup only blocks SIGHUP — it does not fully daemonize the process. After the shell exits, the process stays alive but gets reparented to init (PID 1).
disown — Use After Starting
disown removes a job from the shell's job table so SIGHUP is no longer delivered when the shell exits. Use it when you forgot to prefix with nohup.
# Check running jobs $ jobs [1]+ Running long-job.sh & # Remove job 1 from the job table $ disown %1 # Remove all jobs $ disown -a
disown -h marks the job to not receive SIGHUP without removing it from the job list — you can still see it with jobs.
$ disown -h %1
Recovering When You Forgot nohup
# 1. If the job is in the foreground, suspend it first Ctrl+Z [1]+ Stopped ./deploy.sh # 2. Resume it in the background $ bg %1 [1]+ ./deploy.sh & # 3. Cut SIGHUP delivery $ disown %1 # 4. Safe to close the terminal
screen — Persist the Entire Session
screen is a terminal multiplexer that virtualizes the entire terminal session. The killer feature is detach and reattach — disconnect, close your laptop, reconnect from a different machine, and find your session exactly where you left it.
# Create a named session and enter it $ screen -S mysession # Do your work inside screen # Detach without killing (screen stays alive in background) Ctrl+A D # Reattach from another terminal or SSH session $ screen -r mysession # List all sessions $ screen -ls
Common screen Keybindings
| Action | Key |
|---|---|
| Detach | Ctrl+A D |
| New window | Ctrl+A C |
| Next window | Ctrl+A N |
| Previous window | Ctrl+A P |
| End session | exit in the last window |
screen vs tmux
tmux offers more features and a more flexible config system. If tmux is available on your server, prefer it. See Getting Started with tmux for a comparison. Use screen when tmux is unavailable or you want the simplest possible setup.
Choosing the Right Tool
| Tool | When | Notes |
|---|---|---|
nohup cmd & |
Before starting | Simple, built-in, output goes to nohup.out |
disown |
After starting | Forgot nohup — no log file created automatically |
screen / tmux |
Either | Detach/reattach, multiple windows, session sharing |
Common mistakes to avoid
- Running a background job without
nohupand then closing the terminal — SIGHUP kills it - Pressing
Ctrl+Dinside ascreenwindow thinking you "closed" screen — you ended the session; useCtrl+A Dto detach instead - Letting
nohup.outgrow without bound — always specify a log path or redirect to/dev/null