Understanding the Nohup Command in Linux
This article provides a concise guide to the nohup
command in Linux, explaining its primary purpose, how it prevents
process termination during remote sessions, and how to use it
effectively. By reading this guide, you will learn how
nohup handles terminal hangup signals, manages command
output, and ensures that critical scripts continue executing seamlessly
even after an SSH connection drops or closes.
The Purpose of Nohup
The term nohup stands for "no hangup." Its primary
purpose is to allow processes and scripts to continue running in the
background even after the user logs out, closes the terminal, or loses
their network connection to a remote server.
When you connect to a remote Linux server via SSH, the shell session
acts as the parent process for any commands you run. When the SSH
session closes, the operating system sends a SIGHUP (Signal
Hangup, signal 1) to all child processes running in that session, which
terminates them by default. Prefixing a command with nohup
instructs the operating system to intercept and ignore the
SIGHUP signal, ensuring the targeted process remains
active.
How Nohup Works
Under standard execution, terminal-bound tasks rely directly on the
active session's standard input (stdin), standard output
(stdout), and standard error (stderr).
When invoked, nohup modifies this behavior:
- Signal Immunity: It configures the process to
ignore the
SIGHUPsignal entirely. - Output Redirection: Because the controlling
terminal will eventually close,
nohupautomatically redirectsstdoutandstderrto a file namednohup.outin the current directory. If write permissions are lacking there, it attempts to write to$HOME/nohup.out. - Detachment: When combined with an ampersand
(
&), it releases the command line prompt back to the user while keeping the script active in the background.
Standard Usage
To run a script that persists after disconnecting from an SSH session, use the following syntax:
nohup /path/to/script.sh &The & operator places the command in the background,
allowing you to continue using the terminal immediately.
To specify a custom destination for the generated output rather than
relying on nohup.out, redirect the streams manually:
nohup /path/to/script.sh > custom_output.log 2>&1 &In this command, > custom_output.log sends standard
output to the specified file, and 2>&1 redirects
standard error to the same destination.
Managing Nohup Processes
Because a nohup process is decoupled from your terminal
session, you cannot stop it using standard keyboard shortcuts like
Ctrl + C once the terminal is closed.
To manage or terminate a running nohup process:
- Locate the process ID (PID) using
psorpgrep:ps aux | grep script.sh - Terminate the process using the
killcommand followed by the PID:kill <PID>
Using nohup is a standard, lightweight method for
executing system maintenance, data backups, and long-running data
processing tasks on remote servers without the risk of premature
interruption caused by unstable network connections or closed SSH
windows.