What it is
nohup is a command-line utility that allows you to run commands or scripts that will continue to run even after you log out of your terminal session. It’s particularly useful for long-running background tasks.
Installation
nohup is a standard Unix utility and is available on virtually all Linux and macOS systems by default. No installation is typically required.
Core Concepts
- HUP Signal: When a terminal session ends, it sends a SIGHUP (hangup) signal to all processes associated with that session. By default, most processes will terminate upon receiving SIGHUP.
nohup’s Behavior:nohupintercepts the SIGHUP signal, preventing the command it’s running from terminating. It also redirects standard output and standard error to a file (typicallynohup.out) if they are not already redirected.
Commands / Usage
Running a Command in the Background and Detaching from Terminal
nohup ./my_long_script.sh &
Starts my_long_script.sh in the background, making it immune to terminal hangup signals. Output is appended to nohup.out.
nohup python3 my_server.py &
Runs a Python web server in the background, ensuring it keeps running after logout.
nohup find / -name "*.log" > /tmp/found_logs.txt &
Runs a find command, redirects its standard output to /tmp/found_logs.txt, and detaches it from the terminal. Standard error will still go to nohup.out.
Redirecting Output and Error
nohup ./my_script.sh > output.log 2>&1 &
Runs my_script.sh in the background, redirecting both standard output and standard error to output.log.
nohup ./my_script.sh > /dev/null 2>&1 &
Runs my_script.sh in the background and discards all its output (stdout and stderr). Useful for processes you don’t need to monitor via logs.
nohup ./my_script.sh & echo "Process started"
Starts my_script.sh in the background and immediately prints "Process started" to your current terminal.
Specifying an Output File
nohup -p my_process.pid ./my_script.sh &
This is not a standard nohup option. nohup itself doesn’t have a -p flag to specify a PID file. The common pattern is to redirect output manually.
Correction: nohup does not have a flag to specify the output file directly. It defaults to nohup.out. You must use shell redirection (>, 2>&1) to control output file location.
Example with a Command String
nohup sh -c 'echo "Starting process"; sleep 60; echo "Done"' &
Runs a short shell command string in the background, detached from the terminal.
Common Patterns
Running a long-running server process
nohup gunicorn --bind 0.0.0.0:8000 myapp:app > app.log 2>&1 &
Starts a Gunicorn web server, logs all output to app.log, and detaches it.
Running a data processing job
nohup python3 data_processor.py --input data.csv --output results.json &
Executes a Python data processing script in the background.
Monitoring nohup.out
nohup ./my_script.sh &
tail -f nohup.out
Starts a script and then continuously monitors its output file.
Finding and killing a nohup process
First, find the process ID (PID):
pgrep -f "my_long_script.sh"
Then, kill it:
kill <PID>
Or force kill:
kill -9 <PID>
Gotchas
- Default Output File: If you don’t redirect
stdoutandstderr,nohupwill create (or append to) a file namednohup.outin the current working directory where you ran thenohupcommand. If this directory is not writable, the command will fail. - SIGHUP Behavior:
nohupprevents the command itself from receiving SIGHUP. However, other signals might still affect it. - No Interactive Input: Commands run with
nohupcannot read from standard input interactively. If your command requires user input, it will likely hang or fail. - Shell Redirection is Key: To effectively manage where output goes, you must use shell redirection (
>,2>&1).nohupdoesn’t offer built-in flags for this. - Backgrounding (
&):nohupalone doesn’t put the process in the background. You almost always need to combine it with the shell’s background operator (&).