nohup: Run Commands Unaffected by Disconnects
Summary
The nohup command allows you to run a command that will continue to execute even after you log out of the terminal or the terminal window is closed. It is particularly useful for long-running processes.
Introduction
The nohup (no hang up) command is a utility in Linux systems that allows you to run a command immune to hangup signals. When you disconnect from a terminal (e.g., by closing the SSH connection), a SIGHUP (hangup) signal is sent to all processes associated with that terminal. nohup prevents the command from receiving this signal, ensuring it continues to run in the background. If the standard output and standard error are connected to the terminal, nohup redirects them to a file named nohup.out in the current directory (or $HOME if the current directory is not writable).
Use case and Examples
Running a Script in the Background
This will runmy_long_running_script.sh in the background. The & symbol places the command in the background, and nohup ensures it continues even after you disconnect. Output will be redirected to nohup.out. Running a Command and Redirecting Output
This command redirects standard output tomy_output.log and standard error to the same file (2>&1). The script will run in the background unaffected by the terminal disconnection. Running a Command with a Specific Priority
This command runs the script with a lower priority (usingnice). nohup still ensures it continues running after disconnection. Output will go to nohup.out. Running a Command without Generating nohup.out
/dev/null, effectively discarding them. Useful when you don't need the output. Commonly used flags
| Flag | Description | Example |
|---|---|---|
--help | Displays help information and exits. | nohup --help |
--version | Displays version information and exits. | nohup --version |
| (None) | Without any flags, nohup simply runs the command immune to hangup signals, redirecting output to nohup.out if needed. | nohup ./my_script.sh |