Understanding Screen
Tags: webdev, programming, aws, linux
Published: 2023-01-17 04:00:41

_The following article explains why you may need a tool like Screen and many of the common commands used with the popular terminal multiplexer. If you’re solely looking for the commands please feel free to skip to them._

Recently I was attempting to host the Node.js Backend of my Spotlist web application (which you can find it [here](https://www.spotlist.net/)). Having previously worked with AWS, I decided to host it on an AWS EC2 instance. I created, configured and spun up a Linux-based instance and with little hassle I was able to access my backend API from my client.

“That was easy”, I thought.

A couple of hours later I shut down my MacBook and closed all of the programs I had running, including all of the terminals that were open. One of those terminals was connected to my AWS EC2 instance as I had used it to install and run my Spotlist backend.

When I next tried to continue testing Spotlist, the backend was no longer responding to my API calls. By closing my terminal connected to the EC2 instance I had also terminated everything running on it, specifically my backend. Initially I thought I’d have to leave my terminal open and connected to the instance forever but almost instantly realised that wasn't practically possible. Instead I went in search of tools that would allow an EC2 instance to persist despite the SSH session being disconnected.

That’s when I found Screen.

> Screen (or GNU Screen) is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells.

In my own, hopefully more understandable, words: screen lets you create and run unlimited console applications, or consoles, in a single terminal. The reason it works for our particular issue is because programs on a screen session will continue to run even when the window is not visible and also when the whole screen session is detached from the users terminal.

This will allow me to create a screen on my EC2 instance, run my backend and close my terminal connected to the instance while knowing that my backend (or any other programs I chose to run) will continue.

## Installation

Screen is installed on all major Linux distributions by default. You can verify this by running the following command.

```
screen -version
```

For full disclosure, my AWS instance is running version 4.01.

If screen is not installed on your device, you can run execute the following command to install it.

```
sudo apt install screen
```

##Creating a screen
Starting a new screen session is as simple as typing the following command:

```
screen
```

We can now begin using this virtual terminal or screen and in my case, I simply ran my backend here.

##Finding screens
We can theoretically create endless screens and therefore to view a list of all open screen sessions, we can use the following command:

```
screen -ls
```

I created several screens and ran the above command, here is the output:

![Sample output for 'screen -ls' command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/flrp2rmj4dmv2pasbxcm.png)

The output tells us that there are 6 screen sessions. It gives us the name of each screen session and indicates whether it is attached or detached.

As the name indicates, attached means some process (for example your terminal) is using that screen session. On the other hand, detached means someone left the screen session running (and therefore didn’t terminate or kill it) but disconnected from it. A screen session being detached does not mean it is terminated or that any programs running on it are terminated, the programs will still run and the screen can be reattached to.

##Attaching and detaching
If you open a terminal window, let's say in an AWS instance, and type screen to create a new screen as mentioned above you will be automatically attached to this screen. You can then run your desired program in the screen and when you want to disconnect from the screen (to close the terminal or open another screen session) you execute the following:

```
screen -d
```

Again, this does not kill the screen session or any programs running on it.

To re-attach to a screen session, you have a few options.

If you only have a single screen session running (but detached) you can attach to it with:

```
screen -r
```

You may have multiple screen sessions running as below:

![Screenshot showing multiple screens](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9p7grbmyfsyz5zpou6sc.jpg)

To attach to a particular one you will need to execute the following command (where ‘9929.pts-0.ip-172–31–88–159’ is the screen name):

```
screen -r 9929.pts-0.ip-172–31–88–159
```

In fact, you don’t need to be this precise and can simply use the first character of the screen name as long as no other screen shares the same first character (in which case you can use the first two…). Therefore the below command would have the same effect:

```
screen -r 9
```

But what if you want to use a screen session as quickly as possible? The following command will connect to the first screen session available or create a new one if none exist:

```
screen -x -R
```

##Naming a screen
Now you may like a name like ‘9929.pts-0.ip-172–31–88–159’, but I prefer a more colloquial name. Luckily Screen GNU allows us to name our screens. The below creates a screen with a custom name, in this case, ‘MyNickName’:

```
screen -S MyNickName
```

##Terminating screens
If you ever need to terminate (or kill) all screen sessions, use:

```
killall screen
```

##Want more?
This article only covers the basics of Screen and it’s most commonly used screen commands. For more commands please visit https://www.gnu.org/software/screen/

If you'd like to get in touch, can find me at [frankpierce.me](https://www.frankpierce.me/) or email me at frank.pierceee@gmail.com