Keep Linux Jobs Running After SSH Disconnect with &, nohup, screen
Learn how to prevent Linux processes from terminating when your SSH session ends by using background execution with &, the nohup command, job control shortcuts like ctrl+z, jobs, fg, bg, and the screen utility, including installation, session management, and practical examples.
1. Introduction
Programmers hate when SSH disconnects cause running programs to exit because the shell sends interrupt signals to the session's processes.
The main reason is that the shell sends interrupt signals to the session's processes, causing them to exit. Two interrupt signals are relevant:
SIGINT : sent by Ctrl+C to actively terminate a program.
SIGHUP : sent when the terminal is closed, the network drops, or the screen is locked.
This article introduces several Linux methods to run background tasks and avoid the above problem.
2. Using & Symbol
Appending & to a command runs it in the background and ignores SIGINT, so Ctrl+C won't stop it, though closing the screen or network still terminates the process.
sh test.sh &3. nohup Command
The nohup (no hang up) command runs a program independently of the terminal; SIGHUP is ignored, so SSH disconnection does not stop the process. Ctrl+C can still terminate it. Typically nohup is combined with & so neither terminal closure nor Ctrl+C stops the job. By default output is redirected to nohup.out.
Basic syntax: nohup Command [Arg ...] [&] Example: Run ./test.sh in background, redirect stdout to out.log and stderr to err.log: nohup ./test.sh > out.log 2>err.log & File descriptor meanings:
0 – stdin
1 – stdout (default redirection command > out.log)
2 – stderr
Redirecting stderr to stdout:
nohup ./test.sh > out.log 2>&1 &4. Job Control (Ctrl+Z, jobs, fg, bg)
If a program was started without & or nohup, you can suspend it with Ctrl+Z, then manage it with job control commands.
4.1 Ctrl+Z
Suspend a foreground job and move it to the background:
[1]+ Stopped ./test.sh4.2 jobs
List current jobs and their statuses:
jobs
[1]+ Stopped ./test.sh
[2]+ Running ./test2.sh &4.3 bg
Resume a stopped job in the background (e.g., job 1):
bg 1
[1]+ ./test.sh &4.4 fg
Bring a background job to the foreground (e.g., job 2):
fg 2
./test2.sh5. screen Command
5.1 Overview
screenis a GNU utility that creates virtual terminal sessions. It allows multiple local or remote command-line sessions to be attached, detached, and reattached, keeping programs running even if the SSH connection drops.
5.2 Installation
yum install screen5.3 Usage
1) Create a new session: screen -S yourname 2) List existing sessions: screen -ls 3) Reattach to a session: screen -r yourname 4) Detach a session: screen -d yourname Or detach and reattach: screen -d -r yourname 5) Terminate a session:
screen -S pid-X quitSource: 一口Linux
Signed-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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
