WEB Advent 2012 / Persistent Terminal Sessions

Have you ever had a remote terminal session running, only to have your connection drop out half way through a large task? Then, you reconnect, not knowing anything about its progress (if any) and current status.

Screen is the solution to this problem. Screen allows you to start terminal sessions that you can disconnect from and resume at any time.

I personally use screen a lot with Node.js web servers, so I can kick off the process, and resume my terminal session to check logs or errors, or restart the process if it died.

This article is an introduction to Screen for the average developer, including a few tips and tricks.

Installing

Screen is a Unix tool, so with Ubuntu, you can use apt-get to install it:

sudo apt-get install screen

It’s also possible it’s pre-installed; just type screen to find out.

Instant win with Screen

When I remote in to a server, I run screen, and it starts a new session (and a window, but more about these later). There’s a blurb about what Screen is, I hit return, and the session is ready.

From here, I might start my process. Let’s say I’m doing a large database export.

Now, I need to detach, so I can cleanly close the remote session or do something else while the export is happening. To do this, I type Ctrl+A, followed by D.

This will leave the session running and detach from Screen, so you’re back to your original terminal prompt.

To resume your Screen session:

screen -r

But, what if my connection drops and closes while I’m inside Screen? When I resume my SSH session, the screen could still be attached, so when I type screen -r, it won’t resume. This is simple to get around; you can detach from outside of the Screen session (and, in our case, immediately resume using -r):

screen -dr

Now that you have a basic understanding of Screen, how about using some of its features? Multiple Screen sessions, multiple windows, naming Screen sessions & windows, setting defaults like scrollback, &c.

Screen commands

When you run Screen more than once, you’ll have multiple active Screen sessions. To list the available sessions:

screen -ls

If you have more than one, you’ll need to indicate the session you want to resume:

$ screen -ls
There are screens on:
    17566.ttys001.remys-mba (Detached)
    18778.ttys001.remys-mba (Detached)
    19014.ttys001.remys-mba (Detached)

$ screen -r 18778

This will resume the second session (identified by its process ID). You can also resume the last session using screen -RR. But, resuming using PIDs is ugly, so let’s name the sessions as we create them using the -S argument:

$ screen -S database-dump
[Ctrl+A D]
$ screen -ls
There are screens on:
    17566.ttys001.remys-mba (Detached)
    18778.ttys001.remys-mba (Detached)
    19014.ttys001.remys-mba (Detached)
    18898.database-dump (Detached)

$ screen -r database-dump

Now that you know how to have multiple (named) Screen sessions, let’s look at multiple windows per session.

Windows

Screen supports having multiple windows inside a Screen session, so you can have one screen and multiple windows, each dealing with specific jobs.

To create a new window inside a Screen session, type Ctrl+A, followed by C.

The initial window is 0, the second is 1, and so on. There are a number of ways to switch windows:

Ctrl+A [n]     // where [n] is the window number
Ctrl+A Ctrl+A  // switch to the last used window
Ctrl+A "       // show a list of all the windows
Ctrl+A A       // change the title of this window

There are lots more key bindings you can use to navigate windows, but they’re beyond the scope of this article.

Tricks

Even if you use Screen in its simplest form (as I usually do), I wanted to share a few tricks I’ve discovered.

Multi-user sessions

To connect two (or more) users to an existing screen session:

screen -rx

Instead of detaching any users attached to the session (which is what would happen if we just used -r), this lets me join the session, and anything I type in my window is echoed to any other users connected to the screen.

Maybe this could be useful for remote support, or perhaps training. I’ve not used this in the wild yet, but it’s certainly a fun idea!

Keeping history

One problem with Screen is the scrollback. If you try to scroll up inside a session, instead of doing what you’d expect, the session itself just scrolls away. To scroll back inside a session, you have to use `Ctrl+a [. Now, you can use your keyboard to navigate up and down (this is actually in copy mode). You can hit escape to return back to the prompt.

What might be more useful than a scrollback is a log of the Screen session, which can be toggled on using Ctrl+A H. This can be enabled by default with a .screenrc.

Screen Defaults

Finally, I’ll leave you with my own .screenrc. Create this file in your home directory to set your own defaults for Screen:

# Always show a status line in the window footer.
hardstatus on
hardstatus alwayslastline
hardstatus string "%{.bW}%-w%{.rW}%n %t%{-}%+w %=%{..G} %H %{..Y} %m/%d %C%a "
            
# Auto-detach session on hangup instead of terminating Screen.
autodetach on 
    
# Turn off the splash intro.
startup_message off

# Enable log on all windows.
deflog on

Other posts