nohup Command Cheat Sheet
nohup stands for No Hang Up. It runs a command immune to hangup signals (SIGHUP), allowing the process to continue running after the user logs out.
Synopsis
nohup COMMAND [ARG]...
Basic Usage
Standard nohup
nohup ./long-script.sh &
nohup.out in the current directory.
3. &: Puts it in the background immediately.
Customizing Output
Redirect to Specific Log File
Avoid nohup.out creation.
nohup python app.py > app.log 2>&1 &
> app.log: Redirect standard output to app.log.
- 2>&1: Redirect standard error to standard output (also app.log).
- &: Background.
Discard Output
If you don't care about the logs.
nohup ./job.sh > /dev/null 2>&1 &
Workflow: "I forgot to use nohup!"
If you already started a process (PID 1234) and want to close the terminal without killing it:
- Pause the job:
Ctrl+Z - Background it:
bg - Disown it:
(The
disown -h %1-hflag prevents SIGHUP from being sent to the job).
Troubleshooting
"nohup: ignoring input"
nohup closes Stdin (Standard Input). If your script waits for user input, it will likely stop or fail.
Fix: Redirect input from /dev/null or a file.
nohup ./script.sh < /dev/null &
Notes
- nohup vs &: Simple
&only backgrounds the task. It does NOT protect it from SIGHUP (terminal closure) unless specific shell options (shopt -s huponexit) are set (default varies by shell).nohupguarantees protection. - nohup vs tmux/screen:
tmuxandscreenare vastly superior as you can reattach to the session later.nohupis just "fire and forget".