Master Linux Background Jobs: &, nohup, and screen Explained
This guide explains how to run Linux commands in the background using &, nohup, and screen, covering their syntax, output redirection, job control shortcuts, and practical examples so you can keep the terminal free for other tasks while processes continue running.
Concept
When working in a terminal you may not want a job to occupy the screen, especially for disk‑intensive processes that should run during off‑peak hours; running them in the background frees the terminal.
& Method
Using [shell] & runs a process in the background, but the job ends when the terminal is closed. Example:
| |<br/>| --- |<br/># ping www.baidu.com output goes to ping.log<br/>root@master-01:~# ping www.baidu.com &>> ping.log &<br/>[1] 1254658<br/># View running process<br/>root@master-01:~# jobs<br/>[1]+ Running ping www.baidu.com &>> ping.log &<br/># Kill the job<br/>root@master-01:~# kill %1nohup Method (recommended)
The & operator submits a job to the background; the job stops when the terminal closes. nohup ignores hang‑up signals, allowing the process to continue after logout.
Syntax
| | |
|---|---|
| | nohup Command [Arg …] [&] |Example:
# Run ping in background, output goes to nohup.out
root@master-01:~# nohup ping www.baidu.com &
[1] 1266070
nohup: ignoring input and appending output to 'nohup.out'
# View jobs
root@master-01:~# jobs
[1]+ Running nohup ping www.baidu.com &
# View nohup.out
root@master-01:~# ll nohup.out
-rw------- 1 root root 1745 Apr 21 19:23 nohup.out
# Kill the job
root@master-01:~# kill %1
root@master-01:~# jobs
[1]+ Terminated nohup ping www.baidu.comRedirect output to a specific file
Syntax:
nohup command >> myout.file 2>&1 &Explanation: command>>out.file appends standard output to out.file; 2>&1 redirects standard error to the same file; the final & runs the command in background.
Example:
# Ping and redirect both stdout and stderr to ping.log
root@master-01:~# nohup ping www.baidu.com >> ping.log 2>&1 &
[1] 1270295
# View ping.log
root@master-01:~# tail -f ping.log
64 bytes from 182.61.200.110: icmp_seq=1 ttl=48 time=6.03 ms
...Switching and Controlling Foreground/Background Jobs
CTRL+Zpauses a foreground job and moves it to the background. bg resumes a stopped job in background; bg %jobnumber resumes a specific job. fg brings a background job to foreground; fg %jobnumber selects a specific job.
screen Method
screenis a full‑screen window manager that multiplexes several shells over a single terminal.
Installation
# CentOS
yum install -y screen
# Ubuntu
sudo apt update -y
sudo apt install -y screenUsage
# Create a new window
screen -S test
# Run a command inside the window
python test.py
# Detach the window (keep it running)
Ctrl+a d # or screen -d
exit # close window
# List windows
screen -ls
# Reattach to a window
screen -r <id>Key shortcuts: Ctrl+a c (create), Ctrl+a w (list), Ctrl+a n/p (next/previous), Ctrl+a 0‑9 (switch), Ctrl+a K (kill), Ctrl+a d (detach).
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.
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.
