What is nohup?
nohup stands for 'No Hang Up', meaning it executes a command while ignoring the hangup signal (HUP signal). Normally, closing a terminal window also terminates the programs running within it, but nohup acts like a shield to prevent this.
3 Key Concepts
- Ignore HUP Signal: Ignores the termination signal (SIGHUP) sent by the system when a user logs out, keeping the process alive.
- nohup.out: If no specific output file is designated, the execution results (output content) are automatically saved to the `nohup.out` file.
- & (Background): nohup alone occupies the terminal. It is common to append `&` to the end of the command to send it to the background.
Main Syntax and Options (Shell)
nohup itself does not have complex options. Instead, it is used by combining the **command to be executed** with **redirection (specifying output direction)**.
1. Basic Execution Configuration
2. Log (Output) Management
3. Help
Generated command:
Try combining the commands.
Description:
`nohup` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Scenario Examples
The most common nohup patterns used in practice.
Most Basic Usage
nohup ./backup.sh &
Executes a script in the background. Output accumulates in `nohup.out`.
Specify Log File Name (Recommended)
nohup python3 app.py > my-app.log 2>&1 &
Saves logs to `my-app.log` instead of `nohup.out`. `2>&1` means to also save error messages to the same file.
Run Without Logs
nohup ./heavy-task.sh > /dev/null 2>&1 &
Discards output to `/dev/null` when output content is not needed or when there's a risk of filling up the disk due to excessive output.
Find Running nohup Processes
ps -ef | grep app.py
Checks running processes using the `ps` command.
Installation
nohup is a fundamental command included in 'GNU Coreutils' and is already installed on almost all Linux distributions.
Verify Installation
Most systems do not require separate installation. You can check if it's installed with the command below.
Check Version
nohup --version
Tips & Cautions
Useful Tips
- What about already running tasks?: If you forgot `nohup` and ran a command directly, you can achieve the `nohup` effect by stopping it with `Ctrl+z`, then switching it to the background with `bg`, and finally using the `disown -h %1` command.
- How to Terminate: Processes run with `nohup` cannot be terminated with `Ctrl+c`. You must find the PID (Process ID) using `ps -ef | grep [name]` and then terminate it with the `kill -9 [PID]` command.
- Disk Space Caution: If a long-running program continuously writes logs to `nohup.out`, the file can become enormous and potentially halt the server. Periodically clear it or use `/dev/null`.