Screen hero

Screen hero

One of the first tools I’ll always install on any Linux system, be it baked in via Packer, installed via cloud init or deployed with Ansible, is a terminal multiplexer, more specifically GNU screen. A terminal multiplexer is a terminal window manager that multiplexes a physical terminal between several processes, typically interactive shells. Simply put multiple shells in one shell and indispensable when you spend a good amount of time on the command line.



Install

Give it a test spin by installing it manually.

On a Debian based system:
$ sudo apt install screen

On an enterprise Linux 8 based system:
$ sudo dnf install epel-release
$ sudo dnf install screen

My first stop after installation, is the screen config file at /etc/screenrc for at least one simple edit:

startup_message off

Because a startup message every time is a bit too much. There are more goodies in there, for instance default startup screens you might like. Check them out.

Visualize

There is at least one other tip I would like to give you regarding the options in the /etc/screenrc. By default, screen is very low profile. When you are in a screen session, you can’t see in the blink of an eye when you’re in one. To visualize and make it more apparent when you are in a session, follow these steps:

  1. Comment all hardstatus entries in the /etc/screenrc
  2. Add the below line to this same config file
  3. Launch a new screen session

hardstatus alwayslastline '%{= G}[ %{G}%H %{g}][%= %{= w}%?%-Lw%?%{= R}%n*%f %t%?%{= R}(%u)%?%{= w}%+Lw%?%= %{= g}][ %{y}Load: %l %{g}][%{B}%Y-%m-%d %{W}%c:%s %{g}]'

Usage

Starting screen is as simple as running:

$ screen

Now you can do yourself a favor, as you can read later, by naming your session while starting it, with:

$ screen -S session_name

You’re now in screen, or a shell in a shell. The real power for me is for long running commands that will be running while I do other things or go home.

Let me explain the simple steps for, let’s say, a database download of 20GB at one of your servers. Normally you login via SSH at your server and start the download, but now you can’t exit the SSH session because it will interrupt your download as well. Screen solves this.

Login at the server:

$ ssh [email protected]

Fire it up:

$ screen -S download

Start the file download from your database server located elsewhere:

$ cd /tmp

$ scp [email protected]:~/var/backups/mybackup.sql .

Your download starts as normal, but we’re in the screen session. The magic key combo now isCTRL-a, which sends commands to the screen application instead of the active shell. You can detach the current screen with the download:

CTRL-a and then d

You’ll end up in the original shell of sandbox.greater.nl. Let’s say you want to go home and check out your download later:

$ exit

logout

Connection to sandbox.greater.nl closed.

localhost $

Reattach

Drive home, have diner, login at your server and reattach your session to see how your download is doing:

$ ssh [email protected]
$ screen -r

With the last command you might discover you even have multiple sessions open:

There are several suitable screens on:

17125.pts-0.thisserver (02/09/2021 01:30:17 PM) (Detached)
16158.download (02/09/2032 01:15:31 PM) (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.

Since we named our session before, it’s easy to see which one we need to reattach. Reattach it, copy and pasting the ID:

$ screen -r 16158

Or even by name:

$ screen -xS download

At any time, in any shell, you can list your screens with:

$ screen -list

You can also run a command immediately in screen by running it like so:

$ screen top

But mind you that when you terminate top, you terminate that session as well, whereas when you start screen, run top and terminate top, the session stays active. The magic key combo CTRL-a can only be used when screen is active, so when you’re in a session. Within a session, you can have multiple windows, a window per long running command if you will. This creates a new window (current one keeps running of course):

CTRL-a and then c

Options

You can not only name your sessions, but your windows as well:

CTRL-a and then A

Now fill in a name and end with enter. Do this for all your long running commands in separate windows. To get a list of all your windows, do this in one of them:

CTRL-a and then "

You can easily switch between them and now you know why naming your windows is also a good idea. Besides this nice list, you can switch to the next en previous windows with:

CTRL-a and then n

CTRL-a and then p

As you can see, there is a big difference between sessions and windows. I see a lot of people find the two combined powerful, but it can be confusing as well. Be mindful of what you are doing. In my short research for this post, I saw that many people appreciate the logging feature. While in a session do:

CTRL-a and then H

To start logging all your commands. You can see at the bottom what your log will be called. In your parent shell you can lookup and cat your log.

To start a session out with logging enabled:

$ screen -L

Thus, in my opinion often the best way to start a screen session is:

$ screen -L -S session_name

Start at login

There is the possibility to start screen immediately at every login. So, when you login on your remote system, you end up in a screen session. Put the below in your ~/.bashrc

# Screen at login
if [[ -z "$STY" ]]; then
screen -xRR login_session
fi

From this point on all normal screen operations apply. A CTRL-a and d detaches, an exit throws you back into the normal SSH session, etc.

Finishing up

This is my second post for Greater and quite shorter than the first. I realize these two posts have not been the most exciting compared to all these cool new technologies from, let’s say, the last 5 years. But I’m a firm believer in getting the basics right. I’m a Linux geek by nature, scavenging the Linux terminal on a daily basis and in the terminal, I’m always using at least 3 things:

  1. Vim
  2. Screen
  3. Git

Well, what about that? We’ve covered the first two, guess what I’ll be posting about next time?

Finally, I realize some people might be laughing their asses off because screen is a dinosaur and the newer tmux has more options, being a terminal multiplexer as well. That’s all about personal preferences. Screen does exactly what I want from a multiplexer and I’m not missing any functionality whatsoever. So, for me screen wins, but be sure to check out tmux!