Lets Talk About Screen

We’re going to pause the walkthrough of the Ubuntu server for a moment and talk about a program I absolutely adore, screen

If you haven’t heard of it, no worries! If you have, chances are you probably like it as much as I do. This program is essentially a session manager for your terminal, and though that may not sound like much, I’d be willing to bet after reading the below you will start using it.

If you don’t have it on your computer, or just want to ensure you do, you can download and install it after you update your repository lists:

sudo apt-get update
sudo apt-get install screen

If you want to start reading about screen on your own, you can read the man page, but it’s pretty straightforward once you learn a few things.

man screen

To start screen, type (surprise, surprise)

screen

You will be greeted with the following

Screen splash page

Screen splash page

After you hit enter or space, you will be greeted with an ordinary looking terminal prompt. that is anything but ordinary. Now that you are running with screen, hit

[Control][A]   [?]

That’s Control+A, then Shift+/

Which will present you a listing of the screen key bindings

Screen key bindings

Screen key bindings

As you can see, there are a lot of features within screen, and I’m only going to cover a few of them below.

First, lets talk about multiple terminals. Say you are editing a config file and need to kill a process. Instead of closing what you are doing and opening htop, you can simply create a new screen by hitting

[Control][A]   [c]

This will spawn a new terminal for you to do your work in, leaving the one editing the config file untouched. To move between your terminals, you can use the following commands.

To go to the next screen:

[Control][A]   [n]

To go to the previous screen:

[Control][A]   [p]

To get a listing of all open screens (arrows to move, enter to bring up that screen):

[Control][A]   ["]

To exit out of screen completely, you can use either of the following:

exit

or

[Control][A]   [k]

Now that we have the basics out of the way, lets get into the good stuff.

Say you are doing something over SSH and your connect drops, you lose all your work, right? Not with screen. The terminal simply disconnects and when you log in, you reattach the screen, with everything intact as you left it. You can simulate this by either force disconnecting (killing) the remote terminal you have open and running, or manually disconnect from your screen session which is useful if you want to, say, close your SSH connection by leave all your scrips/config files/etc. running/open.

To disconnect from the session, you use the following command:

[Control][A]   [d]

This takes you back to your base terminal, leaving screen running disconnected in the background. To see the screen sessions open and connected or disconnected on your system, use the following command.

screen -ls

Which will give you output similar to what is shown below.

screen -ls

screen -ls

See how top line shows that I disconnected from the session, then after entering the command above, it shows that I have one disconnected session? To reconnect to that session, I would issue the following command (your screen session name will be different from mine, and don’t forget about tab completion!)

screen -r 1160.tty1.myubuntuserver

An viola! I’m back where I was before my internet went down, killing my SSH connection.

Another thing I find useful is when running verbose backups, copies, etc. Thousands upon thousands of lines will scroll past, sometimes for hours. I don’t want to have to stare at all that, I’d rather do other work. But I don’t want to keep checking on that screen to see when it is finished running either; that’s a waste of my time. Instead, you can monitor for activity or silence and be prompted when either happens.

To monitor for activity (or turn off monitoring if you have it on), you use:

[Control][A]   [M]
activity-yes

Activity monitoring on

activity-no

Activity monitoring off

To monitor for silence (or turn off monitoring if you have it on), you use:

[Control][A]   [_]
Silence monitoring on

Silence monitoring on

Silence monitoring off

Silence monitoring off

When the alerts trip, they will appear as a small text bar at the bottom as well:

Silence alert

Silence alert

The last feature I will cover here is logging. To log the input and output of a screen, use the following command (enter it again to stop logging)

[Control][A]   [H]

This will create a log called screenlog.x where x is the number of the screen window open. This log will be placed in the window’s default directory. Note that existing logs with the same number will be appended by subsequent logging.

Hope this helps make running SSH sessions more efficient and manageable! After a few uses, the commands will be second nature and you’ll wonder how you ever managed without screen.

Tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.

Protected by WP Anti Spam