Master Linux Background Tasks: From &, nohup to screen for Reliable Jobs
This guide explains how to keep Linux processes running despite SSH disconnections or terminal closures by using &, nohup, job control commands, and the screen utility, complete with syntax examples and practical tips.
1. Problem Introduction
Programmers often lose running programs when the SSH session is interrupted by network loss, closing the terminal, or pressing Ctrl+C, because the shell sends interrupt signals that cause the processes to terminate.
The two main signals are:
SIGINT – sent by Ctrl+C to actively terminate a program.
SIGHUP – sent when the terminal is closed, the network disconnects, or the screen is locked.
2. Using the & Symbol
Appending & to a command runs it in the background and ignores SIGINT, but the process still exits if the terminal is closed or the network drops.
sh test.sh &3. nohup Command
nohup(no hang up) detaches a command from the terminal, ignoring SIGHUP so the process continues after SSH disconnects. Ctrl+C can still stop it. Combining nohup with & makes the job survive both terminal closure and Ctrl+C. By default output is written to nohup.out.
Basic syntax
nohup Command [Arg ...] [&]Example
Run ./test.sh in the 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 both stderr and stdout to the same file:
nohup ./test.sh > out.log 2>&1 &4. Job Control: ctrl+z, jobs, bg, fg
If a command was started without &, you can suspend it with Ctrl+Z, then manage it with job control commands.
4.1 ctrl+z
Suspends the foreground job and moves it to the background in a stopped state.
[1]+ Stopped ./test.sh4.2 jobs
Lists current jobs and their statuses.
jobs
[1]+ Stopped ./test.sh
[2]+ Running ./test2.sh &4.3 bg
Resumes a stopped job in the background.
bg 1
[1]+ ./test.sh &4.4 fg
Brings a background job to the foreground.
fg 2
./test2.sh5. screen Command
5.1 Introduction
GNU Screen creates virtual terminals that persist after the SSH connection ends, allowing programs to keep running.
5.2 Installation
yum install screen5.3 Usage
Create a session: screen -S yourname List sessions: screen -ls Resume a session: screen -r yourname Detach a session: screen -d yourname or screen -d -r yourname Kill 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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
