GNU Screen For Fun and Profit

My friends and fellow virtualization professionals Tim Oudin & Joep Piscaer & Paul Gear and some commenters reminded me that the perfect complement to SSH session instability is the GNU utility ‘screen’. Indeed, I’ve been using it for years, and I recommend it to everybody doing any significant work in an SSH session. As the FSF puts it, “screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells.” In short, it virtualizes your interactive shell session so you can disconnect from it and reconnect to it as needed.

On Linux hosts it usually comes as a package, else you can download it from and compile it. I’ve built it on DEC UNIX and AIX in the past. When you run it you’ll get your shell, and all will look normal. A quick check of “who” shows that you’ve got a strange terminal:

$ who
plankers pts/0        Nov  3 15:23 (sandbenders.lab)
plankers pts/3        Nov  3 20:12 (:pts/1:S.0)

Beautiful. So what can you do with this thing? Well, to try it out start a command, like “watch -n 2 date.” That should get you a screen where every two seconds it displays the current time. Let that run as demo.

Now hit Ctrl-A, then an immediate ‘d’. It should say “[detached]” at the bottom of your session. You’ve now disconnected yourself from the screen session. It’s still running in the background, though.

Reconnect with “screen -r”. Ain’t that cool?

Now open another terminal session to the same host, and run “screen -d -r”. That will disconnect it from your other session, then reconnect it to you. You can also just use “-d” to disconnect it, if you want.

You can quit screen in a bunch of different ways, but usually just exiting the shell you’re in inside screen works the best.

There are all sorts of options from the command line, check the man page. From inside screen there are a number of options, too. If you use Ctrl-A then an immediate ‘?’ you will get the help screen.

Want a log of what you’re doing? Ctrl-A then H to toggle it on and off.
Want a screenshot? Ctrl-A then h.
Both of these are great for those “I’m gonna need this for my notes” moments.

Ctrl-A then ‘c’ creates another window within screen, and you can get to it with Ctrl-A then ‘n’. Ctrl-A then ” (double quotes) gets you a nice list of your windows. If you have multiple windows one of the neat things you can do with them is monitor them for inactivity or activity. Waiting for a long compile or something? Ctrl-A then ‘_’ (underscore) monitors for 30 seconds of silence, and it’ll alert you when your other window stopped moving. Ctrl-A then ‘M’ does the opposite and monitors for activity. I sometimes use that when I’m pinging a remote host that’s rebooting.

It’s also worth checking out some of the other references and articles out there. Both Red Hat and IBM DeveloperWorks have good articles with some excellent examples, including how to set up your .screenrc to automatically run commands.

Grepped, a commenter on my SSH keepalive post, also suggests tmux as a screen-like terminal multiplexor. I have no experience with it but it might be worth checking out (and it might be present in places where screen isn’t).

5 thoughts on “GNU Screen For Fun and Profit”

  1. Using byobu on top of screen makes life even better. F2 to create a new screen, F3 and F4 flip left and right through the deck. A nifty info window at the bottom of the window with useful system info. As great as screen is by itself, it’s that much better with byobu.

  2. I use screen religiously at work. My mostly used tip is to turn on multiuser mode and just connect from multiple terminal windows. In my multiple monitor setup, I can have two separate terminals hooked into the same screen session so no worrying about “real” window has a screen window I’m looking for. Multiuser mode is also good for letting the junior guy watch what you’re doing and not letting him type anything (depending on how you define your ACLs) – good for nightime maintenance when neither of you are actually in the office.

    Oh and the benefits are obvious if you lose your net connection as much as I do here with Comcast.. 😉

Comments are closed.