Operations 7 min read

Master Linux Background Tasks: nohup, &, ctrl+z, jobs, bg, fg, and screen

This guide explains how to keep Linux programs running after SSH disconnection by using techniques such as appending '&', employing 'nohup' with output redirection, managing jobs with ctrl+z, jobs, bg, fg, and leveraging the 'screen' utility to create detachable sessions.

Open Source Linux
Open Source Linux
Open Source Linux
Master Linux Background Tasks: nohup, &, ctrl+z, jobs, bg, fg, and screen

1. Problem Introduction

Developers often lose running programs when the SSH session is terminated due to network issues, closing the terminal, or pressing Ctrl+C, because the shell sends interrupt signals that cause processes to exit.

The two main signals are:

SIGINT – sent by Ctrl+C to actively terminate a program.

SIGHUP – sent when the terminal is closed or the network disconnects.

This article introduces several methods to run Linux tasks in the background and avoid these problems.

2. Using the & Symbol

Appending '&' to a command makes the started program ignore SIGINT, so Ctrl+C will not stop it, though closing the terminal or network disconnection will still terminate the process.

sh test.sh &

3. The nohup Command

'nohup' (no hang up) runs a command immune to SIGHUP, allowing it to continue after the SSH session ends. Ctrl+C can still stop the process. Typically, output is redirected to a file named 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 numbers:

0 – stdin

1 – stdout (default redirection command > out.log)

2 – stderr

Redirecting stderr to stdout:

nohup ./test.sh > out.log 2>&1 &

4. Job Control: ctrl+z, jobs, bg, fg

If a program was started without '&' or nohup, you can still manage it using job control commands.

4.1 ctrl+z

Suspends the foreground job and moves it to the background:

[1]+ Stopped ./test.sh

4.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 (e.g., job 1):

bg 1
[1]+ ./test.sh &

4.4 fg

Brings a background job to the foreground (e.g., job 2):

fg 2
./test2.sh

5. The screen Utility

5.1 Overview

'screen' creates a virtual terminal session that persists after the SSH connection ends, allowing programs to keep running.

5.2 Installation

yum install screen

5.3 Usage

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 current and reattach)

Terminate a session:

screen -S pid-X quit
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.

Linuxbackground tasksscreennohup
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

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.