Complete Guide to Screen command on Linux


Reading time: 20 minutes | Coding time: 10 minutes

Screen is an application in Linux system which is used to manage terminal sessions and run processes even when the terminal screen is closed. In this guide, we have demonstrated all screen commands so that you can use it in your daily work.

We have covered the most common use of screen command that is running processes in background as well.

Install Screen

We can install screen application on Linux versions like Ubuntu as follows:

sudo apt-get install screen

For other systems like RedHat (RHEL), we can use:

sudo yum install screen

Start a screen session

To start a screen session, use the following command:

screen

This will open a new session with a clear screen. We can work on this screen and use the features provided by screen application for it.

Get help of screen

To get the help options of screen application, press the following keys in order:

Control key + A key  followed by ? key

Note: for this to work, you should be within a screen session.

Following is the output:

                    Screen key bindings, page 1 of 2.

                    Command key:  ^A   Literal ^A:  a

break       ^B b         license     ,            removebuf   =
clear       C            lockscreen  ^X x         reset       Z
colon       :            log         H            screen      ^C c
copy        ^[ [         login       L            select      '
detach      ^D d         meta        a            silence     _
digraph     ^V           monitor     M            split       S
displays    *            next        ^@ ^N sp n   suspend     ^Z z
dumptermcap .            number      N            time        ^T t
fit         F            only        Q            title       A
flow        ^F f         other       ^A           vbell       ^G
focus       ^I           pow_break   B            version     v
hardcopy    h            pow_detach  D            width       W
help        ?            prev        ^H ^P p ^?   windows     ^W w
history     { }          quit        \            wrap        ^R r
info        i            readbuf     <            writebuf    >
kill        K k          redisplay   ^L l         xoff        ^S s
lastmsg     ^M m         remove      X            xon         ^Q q


                 [Press Space for next page; Return to end.]

Exit a screen session

To exit a screen session, we can detact it using the following keys:

Control key + A followed by d

The output will be like:

[detached from 129921.pts-1.localhost]
(base) [opengenus@localhost ~]$

129921.pts-1.localhost is the name of our screen session and we can use this name to get back into it.

This will allow us to get back into this screen session later and the processes within it will keep running on the system. We will see later in section "Delete a screen session" to learn how we can delete it and terminate all processes within it.

List existing screen sessions

Once we are out of a screen session, we may go back into it. For this, we shall use the following command:

screen -ls

In our case, following is the output:

There are screens on:
        129921.pts-1.localhost  (Detached)
        120455.pts-1.localhost  (Detached)
2 Sockets in /var/run/screen/S-opengenus.

We have two screen sessions namely:

129921
120455

We can go back into one of the sessions as well.

Run a command in a screen session

The advantage of using screen is that you can run a command in a screen session and then, close the terminal and even then, the process will continue to run. It is useful if you are working on a remote machine or need to close the terminal.

The flow will be as follows:

// create a screen session
screen

// run the command now in background
command &

// exit the screen
control key + A followed by d

// now you can close the terminal safely
exit

Go back into a session

To go back into an existing screen session, we should get the name of the session (say 129921) using the list screen command and use the following command:

screen -r 129921

This will give you access to the session.

How to know you are in a screen?

The simplest way to detect is to do the screen list command and if any screen session is active, it may signify that you or some other user is within it.

screen -ls

Alternatively, you can try to exit a screen as well. If you within a screen, it will get detacted and if you are not in one, it will not work.

control key + A followed by d

Delete a screen session

To delete a screen session, we need to get into the screen session we want to delete and following it, there are two options:

  • Use exit command as:
exit
  • Use the following command:
Control key + A followed by k

A prompt will come as:

Really kill this window [y/n]

On pressing y key, the screen session is terminated with the following message:

[screen is terminating]
(base) [opengenus@localhost ~]$

This will terminate the session and kill all process within it.

Name a screen session

We may need to name a screen session so that when we list the sessions, we can understand which session is for which task.

To do this, during the creation of a screen session, we can use the following command:

screen -S name

This will create a screen session named "name" and following is the output of the screen list command:

There are screens on:
        113050.name     (Detached)
        105349.pts-1.localhost  (Detached)
        129921.pts-1.localhost  (Detached)
        120455.pts-1.localhost  (Detached)
4 Sockets in /var/run/screen/S-opengenus.

Lock your screen session

If you want to lock your screen session, you can use the following key combination:

control key + A followed by x

This will turn the screen into this:

Screen used by opengenus <opengenus> on localhost.
Password:

One can enter the UNIX user password to log back into the session.

Log screen session activity

We can log all activity in a screen session by creating a session using the following command:

screen -L

If one is already in a screen session, one can use the following key combination:

control key + A followed by H

This will save a file with all activity.

There are several other options available with screen application. You may go through the help option that we have demonstrate previously and try out each option. In fact, the options we have demonstrate are enough to use screen like a master.

Enjoy this new power.