Learn to use Screen command (with examples)

Screen command is a very important command that provides the ability to launch & use multiple shell sessions from a single ssh session. When a process is started with ‘screen’, we can detach that process from session & than can reattach the session at a later time.

This is extremely useful for the cases where we need to run a process that will for a long time & a network disruption is expected. In this tutorial, we will learn to install screen command & will learn to use screen command with examples.

Installation

CentOS/RHEL

Ubuntu/Debian/LinuxMint

# apt-get install screen –y

Fedora

# dnf install screen –y

Screen command with examples

Start a screen session

To start a screen session, open terminal & run the following command

# screen

This will start a screen session & all the commands executed after this will be executed in screen session. We can also start a screen session with the command that need to be executed in screen session,

# screen ./test.sh

Starting a named screen session

To start a named session, we will use ‘S’ option followed by session name,

# screen –S http_install

This is especially useful when we are dealing with multiple screen sessions.

Deattach screen session

To deattach the current session , we need to press ‘Ctrl + A + D’ keys from the keyboard.

Attach a session

After a session has been deattached, we can attach it again. To do this, first run the following command to check the list of deattached screen session,

# screen –ls

We can than reattach the screen session with ‘r’ option followed by either session name or session number,

# session –r 11335

here 11335 is my session number, replace it with your session number.

Or

# session –r –S http_install

Using multiple screen session

We can use multiple screen session at any time. We can either create a new session after detaching the current screen session or we can create a new screen while current session is underway. This is called nested screen.

To make use of nested screen, we will create a new session by pressing ‘Ctrl + A + c’ & than can shuffle between the session using the key combination ‘Ctrl + A + N’ to goto next screen and ‘Ctrl + A + P’ keys to goto the previous sessions.

This was our brief tutorial on how to use screen command with examples. Please do let us know if you have any questions/queries using the comment box below.

If you think we have helped you or just want to support us, please consider these :-