Linux Command Reference ยท Processes and Signals
Linux nohup command
Reference for the Linux nohup command: keep a program running after you log out, where its output goes, nohup.out, redirection, and the difference from disown and setsid.
nohup runs a command with the SIGHUP signal ignored. When you close a terminal or log out, the kernel and shell send SIGHUP (hangup) to the programs that were started from it, and by default that ends them. A program started with nohup ignores the signal and keeps running.
It also makes sure output has somewhere to go. If standard output is a terminal, nohup redirects it to a file named nohup.out (or $HOME/nohup.out); if standard error is a terminal it is sent to the same place. Add & to also get your prompt back.
nohup COMMAND [ARG]... [&]
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
When you run nohup long-job & in a real terminal, the shell prints nohup: ignoring input and appending output to 'nohup.out' and the program's output lands in that file. The live runner here has no terminal, so the example above shows the redirected case instead.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
nohup COMMAND [ARG]... nohup --help nohup --version
nohup has no real options. Everything after its name is the command to run.
| Situation | What happens |
|---|---|
| stdout is a terminal | Output is appended to nohup.out; a message nohup: ignoring input and appending output to 'nohup.out' goes to stderr. |
| stdout redirected | Left alone; no nohup.out. |
| stdin is a terminal | Replaced by an unreadable descriptor so the program cannot wait for typing. |
| command cannot run | Exit status 126 (found but not executable) or 127 (not found). |
Returns: the exit status of the command, otherwise 126 or 127 as above.
Usage notes
- Only
SIGHUPis ignored.SIGTERM,SIGINTandSIGKILLstill stop the program. - Start it in the background with
nohup cmd &; without the&it still occupies your terminal until it ends. - Redirect explicitly for long jobs:
nohup cmd > out.log 2>&1 &gives a predictable log name and avoids surprises when the current directory is read-only. - Already running?
nohupmust be applied at start. For a job in progress, usedisown -h %1in Bash, which tells the shell not to send itSIGHUP. - Pipelines and functions:
nohupruns a program, not a shell function or a pipeline. Wrap it:nohup sh -c 'a | b' &. - Better tools for services:
setsid(new session),tmuxorscreen(you can reattach), or asystemdunit for something that must start at boot and restart on failure. - Subtle: child processes inherit the ignored signal, unless they install their own handler.
Related
jobs,bgandfg,kill,timeout- Lessons: Jobs and background, Kill and signals