nohup Command
The nohup command in Linux is used to run commands or scripts that will continue to execute in the background even after you log out from the shell or close your terminal. It achieves this by preventing the process from receiving the SIGHUP (hangup) signal, which is typically sent to processes when their controlling terminal is closed.
Syntax
Description
When a command is run with nohup, it automatically redirects its standard output and standard error to a file named nohup.out in the current directory, unless explicitly redirected elsewhere. This ensures that any output from the command is captured even after the terminal session ends.
Common uses include:
- Running long-running scripts or applications in the background.
- Starting services that should persist after user logout.
- Ensuring processes continue uninterrupted by network disconnections.
Common Usage
| Usage | Description |
|---|---|
nohup command & |
Run command in background, immune to hangup, output to nohup.out. |
nohup command > output.log 2>&1 & |
Run command in background, redirect all output to output.log. |
Examples
Run a script in the background
# Output: [1] 12345 # nohup: ignoring input and appending output to 'nohup.out'
Starts my_long_script.sh in the background, and its output will be saved to nohup.out.
Run a Python script and redirect output
Runs a Python application in the background, redirecting both standard output and standard error to app.log.
Check if a nohup process is running
Uses ps and grep to find the running process associated with the script.
Kill a nohup process
Terminates the nohup process using its Process ID (PID).