Operations 6 min read

How to Keep Linux Jobs Running After SSH Disconnects

This guide explains why SSH sessions and terminal closures cause running programs to terminate, describes SIGINT and SIGHUP signals, and provides practical Linux techniques—including using '&', nohup, job control commands, and GNU Screen—to run background tasks that survive disconnections.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How to Keep Linux Jobs Running After SSH Disconnects

Problem Introduction

Programmers often lose long‑running jobs when the SSH session ends, the screen is locked, or Ctrl+C is pressed. The default shell sends an interrupt signal to the process group attached to the terminal, causing the processes to exit.

Relevant Signals

SIGINT : generated by Ctrl+C, forces the program to terminate.

SIGHUP : sent when the terminal is closed, the network disconnects, or the screen is locked.

Methods to Run Jobs in Background

1. Using '&' Operator

Appending '&' to a command starts it in the background and makes it ignore SIGINT, but it still terminates on SIGHUP.

sh test.sh &

2. Using nohup

nohup

(no hang‑up) catches SIGHUP and redirects output to nohup.out by default, allowing the command to continue after the SSH session ends. Ctrl+C still stops the process, so it is common to combine nohup with '&'. nohup Command [Arg ...] [&] Example – run ./test.sh with stdout to out.log and stderr to err.log:

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

Redirecting both streams to the same file:

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

3. Job Control Commands (Ctrl+Z, jobs , bg , fg )

When a process is started in the foreground without '&', you can suspend it with Ctrl+Z, list suspended or running jobs with jobs, resume it in the background with bg, or bring it back to the foreground with fg.

[1]+ Stopped ./test.sh
jobs
[1]+ Stopped ./test.sh
[2]+ Running ./test2.sh &
bg 1
[1]+ ./test.sh &
fg 2
./test2.sh

Using GNU Screen

Installation

yum install screen

Basic Workflow

Create a new session: screen -S yourname List existing sessions: screen -ls Reattach to a session: screen -r yourname Detach a session: screen -d yourname or screen -d -r yourname Terminate a session: screen -S pid-X quit Screen creates a virtual terminal that remains alive even if the SSH connection drops, effectively preventing SIGHUP from terminating the processes running inside the screen session.

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 managementSSHscreennohupbackground jobs
Liangxu Linux
Written by

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.)

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.