How to Use Screen on Linux

Screen on Linux is a popular command-line program that lets you multiplex between interactive shells.

What that means is, you can use screen to save and restore running of commands.

Screen in action

The best way to test this out is to SSH into a server.

sh [email protected]

user@server:~#

At this stage, you can execute man screen to learn all the commands available to you.

You can issue a screen -list to show all screen sessions that may already be open.

$ screen -list

No Sockets found in /run/screen/S-user.

If there are no sessions, then you can simply begin by typing screen and hitting Return.

You will be presented with this the first time around, you can press Space or Return to continue at any time.

Now you can execute some long-running script or otherwise over SSH and not worry about being timed-out, logged out or cut off midway.

Simply press Ctrl+A followed by Ctrl+D to exit out while saving the state.

Listing, Retrieving and Destroying Shells

Now in the parent shell, type screen -list one more time to see the shell still active, that can be rejoined.

$ screen -list

There is a screen on:
	3560.pts-0.server	(02/09/20 09:21:12)	(Detached)

If there is only one entry in this list, type screen -r to rejoin it, otherwise, type screen -r <PTRR_ID>.

For example in the above screen -r 3560.pts-0.server.

To terminate a shell session, simply go into and press Ctrl+D to destroy it. Now if you type screen -list once again, there will be no sessions, just how it started out.