OMG choices, choices. Almost a year ago my colleague introduced me to screen: a cli/terminal window manager that multiplexes a physical terminal between several processes, typically interactive shells.
Simply put: multiple shells in one shell. But there is at least one application that is said that does it better: tmux. So we’ll be using that one, right?
As you might have guessed from the title, I’m using and posting about screen anyway. I like it a bit more although it has less options, but I’m not using these options anyway. I only use it for the stuff I’m going to explain below.
I’m installing it on Debian 9.6 but any Linux will do, I believe all have it in their default repo.
$ sudo apt install screen
My first stop after installation, is the screen config file at /etc/screenrc for at least one simple edit:
Because a startup message every time is a bit too much… There is more goodies in there, for instance default startup screens you might like. Check them out.
Starting screen is as simple as running:
Now you can do yourself a favour, as you can read later on, by naming your session while starting it, with:
$ screen -S sessionsname
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 my 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 firstname.lastname@example.org
Fire it up:
$ screen -S download
Start the file download from your database server located elsewhere:
$ cd /tmp
$ scp email@example.com:~/var/backups/mybackup.sql .
Your download starts as normal, but we’re in the screen session. The magic key combo now is CTRL-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 thisserver.domain.com. Let’s say you want to go home and check out your download later:
Connection to thisserver.domain.com closed.
Drive home, have diner, login at your server and reattach your session to see how your download is doing:
$ ssh firstname.lastname@example.org
$ 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/02/2018 01:30:17 PM) (Detached)
16158.download (02/02/2018 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 particular 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
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.
Last but not least, 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 sessionname
Update: Check out the VI series as well for some nice tips.