Run Linux Commands in the Background Without Losing Them
This guide explains why foreground processes terminate when SSH disconnects, introduces signal types SIGINT and SIGHUP, and provides practical methods—including using &, nohup, job control commands, and GNU Screen—to keep Linux tasks running reliably in the background.
Why background processes exit
When a terminal session ends (e.g., network loss, screen lock, or CTRL+C), the shell sends interrupt signals to the processes attached to that session, causing them to terminate.
SIGINT : generated by CTRL+C, forces a program to stop.
SIGHUP : sent when the terminal hangs up (e.g., SSH disconnect), also terminates the process.
Method 1: Append & to the command
Placing & at the end of a command starts it in the background and makes it ignore SIGINT. The process still receives SIGHUP, so it will stop if the session is closed.
sh test.sh &Method 2: Use nohup
nohup(no hang‑up) runs a command immune to SIGHUP. The process continues after the SSH session ends, but CTRL+C can still stop it. Combining nohup with & protects against both signals. nohup Command [Arg ...] [&] Example that redirects output: nohup ./test.sh > out.log 2>err.log & File descriptor meanings:
0 – standard input
1 – standard output (default for > out.log)
2 – standard error
Redirect both streams to the same file:
nohup ./test.sh > out.log 2>&1 &Method 3: Job control ( CTRL+Z , jobs , bg , fg )
If a command was started in the foreground without &, you can pause it, move it to the background, and later resume it.
CTRL+Z : Suspends the foreground job and marks it as stopped. [1]+ Stopped ./test.sh jobs : Lists current jobs and their statuses.
jobs
[1]+ Stopped ./test.sh
[2]+ Running ./test2.sh &bg : Resumes a stopped job in the background.
bg 1
[1]+ ./test.sh &fg : Brings a background job to the foreground.
fg 2
./test2.shMethod 4: GNU screen
screencreates a virtual terminal that persists after the SSH connection drops, allowing you to detach and reattach sessions at will.
Installation
yum install screenTypical workflow
Create a new session: screen -S yourname List sessions: screen -ls Reattach to a session: screen -r yourname Detach a session: screen -d yourname (or screen -d -r yourname to end the current session and reattach)
Terminate a session:
screen -S pid-X quitSigned-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.
