Master Linux Background Jobs: &, nohup, and screen Explained
This guide explains how to run commands in the background on Linux using the '&' operator, the nohup command, and the screen utility, covering job control, output redirection, process termination, and practical examples with full command syntax.
Background
When a long‑running or disk‑intensive job should continue after you log out or during off‑peak hours, it is useful to run the job in the background so the terminal remains free.
Running a command in background with &
Appending & to a command starts it as a background job. The job is terminated when the terminal session ends.
# ping www.baidu.com >> ping.log &
[1] 1254658
# jobs
[1]+ Running ping www.baidu.com >> ping.log &
# kill %1nohup (recommended)
nohupignores the SIGHUP signal, allowing the command to keep running after logout.
# nohup ping www.baidu.com &
nohup: ignoring input and appending output to 'nohup.out'
# jobs
[1]+ Running nohup ping www.baidu.com &
# kill %1Typical syntax: nohup Command [Arg …] [&] Example with output redirection:
# nohup ping www.baidu.com >> ping.log 2>&1 &
[1] 1266070
# tail -f ping.log
64 bytes from 182.61.200.110: icmp_seq=1 ttl=48 time=6.03 msJob control commands
CTRL+Z– Suspend the foreground job and move it to the background. bg – Resume a suspended job in the background. bg %jobnumber – Resume a specific job in the background. fg – Bring the most recent background job to the foreground. fg %jobnumber – Bring a specific job to the foreground.
screen utility
screenis a full‑screen terminal multiplexer that lets you create detachable sessions.
Installation
# CentOS
yum install -y screen
# Ubuntu
sudo apt update -y
sudo apt install -y screenBasic usage
# Create a new session named test
screen -S test
# Run a command inside the session
python test.py
# Detach (keep it running)
Ctrl+a d # or: screen -d
# List existing sessions
screen -ls
# Reattach to a session
screen -r testUseful shortcuts
Ctrl+a c – Create a new window
Ctrl+a w – List windows
Ctrl+a n – Switch to next window
Ctrl+a p – Switch to previous window
Ctrl+a 0‑9 – Switch directly to window 0‑9
Ctrl+a K – Close current window and move to next
Ctrl+a d – Detach from the sessionSigned-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.
