Operations 8 min read

Master Linux Background Jobs: &, nohup, and screen Explained

This article explains how to run Linux commands in the background using &, nohup, and screen, covering concepts, syntax, practical examples, job control shortcuts like CTRL+Z, bg, fg, and detailed instructions for installing and using screen to manage persistent sessions.

Raymond Ops
Raymond Ops
Raymond Ops
Master Linux Background Jobs: &, nohup, and screen Explained

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; therefore you need ways to run them in the background.

& Method

Using [shell] & runs a process in the background, but the job ends when the terminal session is closed.

Example:

# ping www.baidu.com >> ping.log &
[1]+  Running    ping www.baidu.com &>> ping.log &
ps -aux | grep ping
kill %1

nohup Method (Recommended)

Appending & submits a job to the background, but it stops when the terminal closes. The nohup command ignores hang‑up signals, allowing the process to continue after logout.

Syntax:

|   |   |
|---|---|
|   | nohup Command [ Arg … ] [&] |

Example:

# nohup ping www.baidu.com &
[1] 1266070
nohup: ignoring input and appending output to 'nohup.out'
jobs
[1]+  Running    nohup ping www.baidu.com &

Redirecting nohup Output to a File

Syntax: nohup command >> myout.file 2>&1 & command>>out.file redirects standard output to the file.

2>&1 redirects standard error to the same file; the final & runs the command in background.

Example:

# nohup ping www.baidu.com >> ping.log 2>&1 &
[1] 1270295
nohup: ignoring input and appending output to 'nohup.out'
... (output of ping.log) ...
kill %1

Switching Between Foreground and Background Processes

Keyboard shortcut CTRL+Z pauses a foreground job and moves it to the background.

Use bg to resume a stopped job in the background, optionally specifying bg %jobnumber. Use fg to bring a background job to the foreground, optionally with fg %jobnumber.

screen Method

Screen is a full‑screen window manager that multiplexes multiple shells over a single terminal.

Installation

# CentOS
yum install -y screen

# Ubuntu
sudo apt update -y
sudo apt install -y screen

Usage

# Create a new window
screen -S test

# Run a command inside the window
python test.py

# Detach the window
Ctrl+a d   # or screen -d
exit       # close the window

# List windows
screen -ls

# Reattach a window
screen -r <id|name>

Related shortcuts:

Ctrl+a c   # create window
Ctrl+a w   # list windows
Ctrl+a n   # next window
Ctrl+a p   # previous window
Ctrl+a 0‑9 # switch between windows 0‑9
Ctrl+a K   # close current window
exit       # close current window
Ctrl+a d   # detach and return to original shell
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Process ManagementLinuxscreennohupjob-control
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.