MySource Matrix Developer

Main Content

Saving Time with GNU Screen

Introduction

Do you find yourself spending the first 10 minutes at work opening up all the different terminals you were working with the day before - or even worse, the terminals you use every day? Wouldn't it be great if you could just fire up one terminal and resume where you left off yesterday, a week ago, or even a month ago?

Written by Unknown LDAP User on 17 Jan 2006

As you've probably guessed, there is a way to do all this. It's a little application called GNU Screen, referred to simply as "screen".

What is Screen?

Here's a little excerpt from the screen man page:

When screen is called, it creates a single window with a shell in it (or the specified command) and then gets out of your way so that you can use the program as you normally would. Then, at any time, you can create new (full-screen) windows with other programs in them (including more shells), kill existing windows...

The description keeps going, but we'll leave it at that for now. I'll throw a few tips into the article later on which will outline some other things you can do with screen.

As the man page implies, screen is essentially a window manager for your terminals. Running screen will create a new window with a shell inside, and within that we can open even more windows as well as manage existing ones.

If you're still here, it's time to get your hands dirty. Soon you'll be looking back thinking, "Damn, why hadn't I heard of this earlier?" I'm starting to sound like I'll throw in a free set of steak knives, so without further ado, "Hi Ho Silver, away!"

Installation

First up, you'll need screen installed on your server (Sorry, *nix servers only...or Cygwin if you're a fence-sitter); aka "the machine you do all your shell work on". This is simply going to be a matter of using apt-get, yum, emerge or <insert your distro's package manager here> to install it. The rest of this article assumes you've got screen installed (generally to /usr/bin/screen, but as long as it's in your path it doesn't really matter).

Now, you're probably staring at an empty shell wondering what this screen program will do for you. Let's find out.

The Basics

Let's start by creating a new screen session:

$ screen

You're now "inside" screen. Think of it as something which is going to wrap itself around everything you normally do in multiple shells.

$ ls

We now have a screen session containing a window that's got the output of ls in it (ls lists the files in the current directory). This is where it starts to get interesting - let's create a new window.

CTRL-a c

OK, that might be a bit confusing. (Almost) everything in screen is done by pressing CTRL-a first. In that last case, we want to "create" a new window, so press CTRL-a, followed by pressing c. Hopefully the window you typed ls in is gone, and you have a fresh new window open.

$ ls -l

Now that we have two different windows with two different outputs (ls and ls -l), we need to be able to open up the first window we had open.

CTRL-a 0

That probably didn't make sense. c was "create", so what's 0? Well, each window within screen gets a number. The first window is number 0, the second is 1, the third is...well, you get it. To switch to a particular window, you press CTRL-a followed by the number of the screen you want.

If you want to go back to the last window you had open, you can also just press CTRL-a a. This toggles between the current window and the last opened window.

This switching between windows is great, but what happens if you don't know that you're running XYZ script in window 6? Trust me, this will start to happen when you keep the same screen session for weeks on end. Luckily, we can pull up a list of all the windows and select which one we want.

CTRL-a "

Even though listing all the different windows in the current screen is great, it would be even better if each window had a readable name. Of course, screen supports this.

CTRL-a A

This will rename the current window.

At this point, you know how to create a screen, add some windows to it, and switch between the windows. There are two things I haven't mentioned:

  1. To exit a window, type exit or press CTRL-d as you normally would.
  2. To close down screen (saving it for later), read on.

Detaching/Reattaching a Screen Session

The process of closing down screen and leaving it in the background ready to be opened again (with all your shells still inside it) is called "detaching". So it won't come as much of a suprise when I tell you how to detach a screen session.

CTRL-a d

You should recieve a message saying "[detached]" or similar. That screen session is now sitting in the background, waiting to be "resumed". If you think back, you'll remember that we started screen by just typing screen. What do we do if we want to start screen and resume where we left off?

$ screen -r

Wait a second, what if we just typed screen without the -r flag like we did at the start of this article? Where would the previous screen session go? Well, it would still be there! You can create multiple screen sessions (don't even think about doing screens within screens 'ya sneaky bugger), and detach / reattach as you please.

To find out a list of detached screens available for reattaching, type screen -ls. To re-attach a specific screen session, specify the name of the session after screen -r.

Simple huh?

This is essentially what you'll be doing each day. Walk into the office, resume your screen session from the day before and start work straight away. At the end of the day, detach it ready for the day after.

Other Uses

  • You start a script and want to run it in the background (like a daemon). Just open screen, start the script and detach it. You can monitor it at any time by reattaching it.
  • You've got multiple shells open logged into the company server and the network goes down. What happens? Unless you really know what you're doing, it's impossible to get back to where you were without opening all the shells again. What if you were running screen? All you'd have to do is reattach and you're off and running again.
  • You server at work as SSH access from both your work computer and the rest of the world. Wouldn't it be great if you could go home, SSH into the server and pick up where you left off at work? All you need to do is detach at work, then reattach at home.

If you forget to detach the screen session you'll be unable to reattach it later on unless you type screen -dr. This forces the screen session to detach before attempting to attach it.

Wrap up

As with all these kinds of applications, you can use them as basically or extensively as you like. Even in its' most basic form, screen helps me get stuck straight into my work every day, which in turn increases the amount of time I've got to spend on work. Once you've used it for a while and start to get comfortable, the possibilities are endless!

Try it, you know you want to.