Overview

When working on remote SSH on your linux machine, you might have a situation that you need perform a long-running task such backup, patch, etc, it requires you to keep the ssh connect until the process is completed.

If your session is intentionally or unintentionally disconnected, then the process will be stopped. To prevent that, you can use a utility called screen that allows to resume the sessions.

What is Screren

Screen is a terminal multiplexer to create multiple virtual terminal on an SSH session. Processes running in Screen will still continue to run even their window is not visible.

Install Linux GNU Screen

To check if screen is installed.

screen --version

Install Linux Screen on Ubuntu and Debian

sudo apt install screen

Install Linux Screen on CentOS and Fedora

sudo yum install screen

Starting Linux Screen

  • Tp get the manual of this screen utitlity.
man screen
  • To start a screen session, simply type screen in your console:
screen

or

screen -S session_name

This will open a screen session, create a new window, and start a shell in that window. Now, you can perform the long-term task on this virtual session.

Detaching Linux Screen Session

To detach your current session, you can use the following:

ctrl+a d

Resume the previous session:

screen -R

If there’s 1 session there, it will automatic connect to the previous session. But if there are multiple virtual sessions there, it will show as the following, similiar use screen -ls to list all screen.

There are several suitable screens on:
        30708.pts-0.lnsrv01   (07/24/20 10:55:49)     (Detached)
        30572.jaja      (07/24/20 10:49:47)     (Detached)
        26380.pts-0.lnsrv01   (07/24/20 10:36:43)     (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.

Use the following command to connect to one of the exited screen session:

screen -r [screen-id]

To kill all screen.

To kill all screen you can use either:

killall screen

or this command that will kill all detached screens.

screen -ls | grep '(Detached)' | awk 'sys {screen -S $1 -X quit}'