Session Control using Screen

Background

Normally when you log out from a session on a Linux system, any processes of yours that are still running get terminated.  When you have long-running jobs, you may wish to log out while leaving those jobs running, and come back later to see how they are doing.

Between nohup and screen, screen is the more complicated of the two, by far, but once learned gives much greater flexibility and control over your sessions.

Starting and Quitting Screen

To start a new screen session, just type screen and hit enter.  You'll see a notice giving you the version and license information, and you can hit enter to dismiss this, and then you will be in a shell session inside screen.

To disconnnect, simply type exit to end the current shell.  You'll see the message [screen is terminating] and you'll be returned back to your normal shelll session.

Disconnecting and Reconnecting from a Screen Session

Instead of quitting screen entirely, you can detach your current terminal from the screen session, leaving any processes you launched running, and reconnect later seeing any output that may have resulted in the interim.  To do so, hit ctrl-a then d.  ctrl-a is the meta key, indicating that you're entering a screen command.  See the section about ctrl-a for more information

To reconnect to an existing screen session, you can type screen -x or screen -r.  Note if you have more than one screen session running you'll need to specify which one you want to reconnect to.  See below for more information.

ctrl-a

All of the commands you want to issue to screen itself while inside a screen session begin with a ctrl-a followed by a further keypress or key combination.  For example as mentioned above, to tell screen you want to detatch leaving screen running, you hit ctrl-a d.  There's a huge number of commands available to you, and you can see a summary of them when attached to a screen session by hitting ctrl-a ?.

Finding Existing Screen Sessions

If you try to reconnect to screen, and you have more than one session running, you must explicitly specify which session you want to reconnect to.

To see a list of all your running screen sessions on the current machine type screen -list (or screen -ls).  If you have running sessions, you'll see something like this:


username@cpu137:~$ screen -list
There are screens on:
        31562.pts-2.cpu137      (06/20/2022 01:21:49 PM)        (Detached)
        31515.pts-2.cpu137      (06/20/2022 01:21:45 PM)        (Detached)
2 Sockets in /run/screen/S-username.

You can then specify which screen to reconnect to using the appropriate value from the first column: screen -r 31562.pts-2.cpu137

Using Multiple Windows in a Single Screen Session

Rather than creating multiple screen sessions, you can create multiple windows within a single screen session and then switch between those windows once you reconnect to your session.

The windows within screen are numbered, starting at 0 for the initial screen.  Additional windows can be created by hitting ctrl-a-c.  You can move forward to the next or backwards to the previous in the series of windows with ctrl-a-n and ctrl-a-p respectively.  To go to a specific window, you can use the window's number and jump to it with ctrl-a-#.  To switch to the most recent screen before your current one, hit ctrl-a-ctrl-a.  To see the list of all the active windows, hit ctrl-a-".

Note that if you want to end a screen session that has more than one window, you must exit out of all the windows associated with that screen session.  Screen will terminate once you exit the final window.

Scrollback/Logging in a Screen Session

You can see any output that has been generated while you've been disconnected from an active screen session, but if more than a window's worth has been generated, then you will only see the last screen full's worth.  To scroll back (up to the size of the output buffer) you can hit ctrl-a-[ or ctrl-a-esc to enter copy mode, then use vi-like movement keys to scroll through the output.  e.g. ctrl-b and ctrl-f to go back or forward a full screen's worth of output, or just j or k to go up or down a single line.  Hit ESC again to return to the normal interactive mode.

Summary

As mentioned at the start of this article, this is just a selection of useful commands to get started using screen.  There is lots more.  hit ctrl-a-? for a quick reference guide within screen or check the man page (run man screen) for the full documentation