How to Keep Linux Processes Running After SSH Disconnects
This guide explains why SSH sessions terminate running programs due to SIGINT and SIGHUP, and demonstrates practical techniques—including using '&', nohup, job control commands, and GNU Screen—to run Linux processes reliably in the background even when the connection drops.
Signal behavior of SSH sessions
When a terminal is closed, the network drops, or CTRL+C is pressed, the shell sends signals to the processes attached to that session. Two signals are relevant:
SIGINT – generated by CTRL+C; it requests a graceful termination of the foreground process.
SIGHUP – sent when the controlling terminal disappears (e.g., SSH disconnect, screen lock, or terminal close). Processes that remain attached to the session receive this “hang‑up” signal and typically exit.
Understanding these signals is essential for keeping long‑running jobs alive after an SSH disconnect.
1. Running a command in the background with &
Appending & to a command starts it as a background job. The job no longer receives SIGINT from CTRL+C, but it still inherits the controlling terminal and will receive SIGHUP if the session ends.
sh test.sh &2. Using nohup to ignore SIGHUP
nohup(no hang‑up) detaches a command from the controlling terminal. It sets the signal handler for SIGHUP to “ignore”, allowing the process to continue after the SSH session ends. CTRL+C (SIGINT) still terminates the process, so nohup is often combined with & for full resilience.
Basic syntax: nohup Command [Arg …] [&] Typical redirection example (stdout to out.log, stderr to err.log):
nohup ./test.sh > out.log 2> err.log &File descriptor meanings:
0 – standard input ( stdin)
1 – standard output ( stdout) – default redirection is 1> 2 – standard error ( stderr)
To merge stderr into stdout:
nohup ./test.sh > out.log 2>&1 &3. Job control primitives
If a command is started in the foreground, you can manipulate it with the following built‑in shell controls:
CTRL+Z – suspends the foreground job and places it in the stopped state.
jobs – lists all current jobs with their status and job numbers.
bg %n – resumes a stopped job %n in the background.
fg %n – brings a background (or stopped) job %n to the foreground.
Example workflow:
# Start a foreground job ./test.sh # Suspend it CTRL+Z # Verify job list jobs # Output (example) [1]+ Stopped ./test.sh # Resume in background bg %1 # Bring back to foreground fg %14. Persistent sessions with screen
GNU Screencreates a virtual terminal that remains alive independently of the SSH connection. Processes started inside a screen session continue to run even if the network drops, because the screen server maintains the controlling pseudo‑terminal.
Installation
yum install screenTypical workflow
Create a new session: screen -S <session_name> List existing sessions: screen -ls Detach from the current session (leaving it running): screen -d <session_name> Reattach to a detached session: screen -r <session_name> Terminate a session: screen -S <session_name> quit Example usage:
# Start a detached session named myjob screen -S myjob # Inside the screen, launch a long‑running command nohup ./long_task.sh > task.log 2>&1 & # Detach (Ctrl+A then D) and safely disconnect SSH # Later, reconnect and reattach screen -r myjobSigned-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.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
