Using screen

Overview

AERPAW sample scripts and sample applications make extensive use of the screen Linux command for managing multiple processes from the same terminal window. Since screen is not a commonly used utility, this page serves as a basic tutorial for the AERPAW Experimenters and Operators.

Screen is a standard Unix utility; the manual page is explicit and long. Good tutorials are available here and here. The write-up below is the minimum necessary to get around on AERPAW.

In short, screen allows a user to use a set of separate "terminals" (called screens) for different processes while keeping the outputs and inputs cleanly separated from each other. In this way, screen is the text/terminal equivalent of having different applications in different windows, which is so common in GUI environments.

screen - A Crash Course

The simplest way to start a process in a new screen (think of a screen as a separate window) is by typing:

$ screen <command>

For example:

$ screen ping www.google.com

This command will apparently clean the current terminal and start the ping process as follows:

PING www.google.com (172.217.0.36) 56(84) bytes of data.

64 bytes from lga15s43-in-f36.1e100.net (172.217.0.36): icmp_seq=1 ttl=117 time=8.27 ms

64 bytes from lga15s43-in-f36.1e100.net (172.217.0.36): icmp_seq=2 ttl=117 time=8.33 ms

64 bytes from lga15s43-in-f36.1e100.net (172.217.0.36): icmp_seq=3 ttl=117 time=8.17 ms

64 bytes from lga15s43-in-f36.1e100.net (172.217.0.36): icmp_seq=4 ttl=117 time=8.20 ms

64 bytes from lga15s43-in-f36.1e100.net (172.217.0.36): icmp_seq=5 ttl=117 time=8.44 ms

The user can then detach from this screen session by typing Ctrl+a Ctrl+d:

$ screen ping www.google.com

[detached from 299384.pts-1.zerg]

$

The two processes (screen and ping) that are started by the screen command above continue in the background. The process id of the screen process is the same as shown by screen when detached (299384 in this example):

$ ps -aef

...

mlsichit 299384 1 0 11:51 ? 00:00:00 SCREEN ping www.google.com

mlsichit 299385 299384 0 11:51 pts/3 00:00:00 ping www.google.com

$

The user can check for the existence of live screens in the background by:

$ screen -ls

There is a screen on:

299384.pts-1.zerg (09/07/2021 11:51:31 AM) (Detached)

1 Socket in /run/screen/S-mlsichit.

$

The user can re-attach to a detached screen by:

$ screen -R 299384

64 bytes from iad23s72-in-f4.1e100.net (172.217.2.100): icmp_seq=433 ttl=117 time=8.56 ms

64 bytes from iad23s72-in-f4.1e100.net (172.217.2.100): icmp_seq=434 ttl=117 time=8.39 ms

64 bytes from iad23s72-in-f4.1e100.net (172.217.2.100): icmp_seq=435 ttl=117 time=8.58 ms

The screen and ping commands continued to run in the background (note the sequence number increase). Detach again by typing Ctrl+a Ctrl+d. Then start a new screen session:

$ screen ping www.cnn.com

PING turner-tls.map.fastly.net (151.101.193.67) 56(84) bytes of data.

64 bytes from 151.101.193.67 (151.101.193.67): icmp_seq=1 ttl=58 time=8.24 ms

64 bytes from 151.101.193.67 (151.101.193.67): icmp_seq=2 ttl=58 time=7.87 ms

64 bytes from 151.101.193.67 (151.101.193.67): icmp_seq=3 ttl=58 time=8.15 ms

64 bytes from 151.101.193.67 (151.101.193.67): icmp_seq=4 ttl=58 time=7.98 ms

64 bytes from 151.101.193.67 (151.101.193.67): icmp_seq=5 ttl=58 time=8.38 ms

64 bytes from 151.101.193.67 (151.101.193.67): icmp_seq=6 ttl=58 time=8.23 ms

64 bytes from 151.101.193.67 (151.101.193.67): icmp_seq=7 ttl=58 time=8.25 ms

64 bytes from 151.101.193.67 (151.101.193.67): icmp_seq=8 ttl=58 time=8.16 ms

64 bytes from 151.101.193.67 (151.101.193.67): icmp_seq=9 ttl=58 time=8.28 ms

64 bytes from 151.101.193.67 (151.101.193.67): icmp_seq=10 ttl=58 time=8.02 ms

And detach from this second screen session by Ctrl+a Ctrl+d:

$ screen ping www.cnn.com

[detached from 299642.pts-1.zerg]

~$

Screen now has two background (detached sessions):

$ screen -ls

There are screens on:

299642.pts-1.zerg (09/07/2021 12:02:16 PM) (Detached)

299384.pts-1.zerg (09/07/2021 11:51:31 AM) (Detached)

2 Sockets in /run/screen/S-mlsichit.

$

You can change between them at will by re-attaching (with -R) to either of them. Upon the termination of the process started in the screen session, the screen process will also terminate and control will be returned to the parent terminal process.

Naming screen Sessions

While it is always possible to retrieve a detached screen session by its process number (or even just with screen -R when only one screen session is in detached), when using multiple detached sessions, it's easier to remember which session is running which process if we name the session:

$ screen -S youtube ping www.youtube.com

[detached from 299772.youtube]

$

Then listing the sessions:

$ screen -ls

There are screens on:

299772.youtube (09/07/2021 12:10:09 PM) (Detached)

299642.pts-1.zerg (09/07/2021 12:02:16 PM) (Detached)

299384.pts-1.zerg (09/07/2021 11:51:31 AM) (Detached)

$

And finally, re-attaching to the youtube session:

$ screen -R youtube

In AERPAW, by default, we name all screen sessions with names that are easy to recognize.

Starting Detached Processes

In AERPAW all scripts start screen sessions in the background. In the development mode (and in the sandbox) the Experimenter can attach to any of the screen sessions running in the E-VM to monitor, and if needed to stop and restart the process running in that screen session. To start a detached screen session, the flags -dm are used:

$ screen -dm -S amazon ping www.amazon.com

$ screen -ls

There are screens on:

300002.amazon (09/07/2021 12:23:05 PM) (Detached)

299772.youtube (09/07/2021 12:10:09 PM) (Detached)

299642.pts-1.zerg (09/07/2021 12:02:16 PM) (Detached)

299384.pts-1.zerg (09/07/2021 11:51:31 AM) (Detached)

4 Sockets in /run/screen/S-mlsichit.

$

Stopping screen Processes

Since screen and the processes started within are standard unix processes, they can always be stopped by using the standard kill signals. However, screen provides an additional method of stopping the processes within a particular screen session:

$ screen -X -S youtube quit

$ screen -ls

There are screens on:

300002.amazon (09/07/2021 12:23:05 PM) (Detached)

299642.pts-1.zerg (09/07/2021 12:02:16 PM) (Detached)

299384.pts-1.zerg (09/07/2021 11:51:31 AM) (Detached)

3 Sockets in /run/screen/S-mlsichit.

$

For additional options (including sharing a screen session with multiple simultaneous users) see the man page or the tutorials.