Skip to content

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 &
1. Prevents SIGHUP: Process won't die when terminal closes. 2. Output: Stdout and Stderr are redirected to a file named 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 &
Breakdown: - > 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:

  1. Pause the job: Ctrl+Z
  2. Background it: bg
  3. Disown it:
    disown -h %1
    
    (The -h flag 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). nohup guarantees protection.
  • nohup vs tmux/screen: tmux and screen are vastly superior as you can reattach to the session later. nohup is just "fire and forget".