How to Use nohup for Reliable Background Execution on Linux
Learn how the nohup command lets Linux processes ignore SIGHUP, run in the background, and continue after terminal closure, including syntax, options, output redirection behavior, and practical examples for reliable background execution.
1. Command Overview
nohup runs a program while ignoring the SIGHUP (hangup) signal. It is commonly used together with '&' to place the command in the background, allowing it to continue running even if the terminal is closed.
There are two typical ways to run a program in the background: command &: runs in background, but stops when the terminal is closed. nohup command &: runs in background and continues after the terminal is closed.
Note:
(1) If nohup is used without explicit standard output redirection, the output is redirected to nohup.out in the current working directory. If that file is not writable, it defaults to $HOME/nohup.out. If no file can be created, the command cannot be executed.
(2) If standard error is not explicitly redirected, it is redirected to the same file as standard output.
2. Command Syntax
nohup COMMAND [ARGS]
nohup OPTION3. Options
--help
Show help information and exit
--version
Show version information and exit4. Common Examples
(1) Submitting a job with nohup; by default all output goes to nohup.out unless another file is specified. nohup ./test.sh & (2) Redirecting both standard output and standard error. nohup ./test.sh > test.log 2>&1 & Note: 2>&1 redirects standard error to the same destination as standard output; the trailing '&' places the command in the background.
References
[1] nohup(1) manual (http://man7.org/linux/man-pages/man1/nohup.1.html)
[2] Linux shell script execution methods and differences & background command (https://blog.csdn.net/heqiyu34/article/details/19089951/)
[3] Linux command collection: nohup command (http://man.linuxde.net/nohup)
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
