In the words of the manpage, "Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells).". What? Well basically what screen allows you to do is run one or more applications in a terminal session which can be 'disconnected' so that it runs in the background. This is different from simply backgrounding a running process because you can retrieve a screen session easily and get full interactive control over it again. OK, you can regain interactive control of backgrounded processes too, using the fg command, but this approach isn't suitable for full-screen console applications like irssi. You can run more than one screen session, but you can also run more than one application simultaneously within a single screen session.

Why is this so useful?

Lets say I want to compile a new kernel on a remote machine, and this is going to take approximately 30 minutes. Rather than stay connected to the machine for the duration of the compile, I can shell in, start the compile in a screen session, disconnect the session and log out. Half an hour later I can shell back in and retrieve the session to see the result of the compile and continue using that session if I wish.

Not convinced? How about console IRC clients? I'm permanently connected to IRC because I run irssi in a screen session. When I want to use it I just ssh to the box on which it runs and reconnect the session and immediately I'm on IRC. I can then disconnect it when I'm finished and IRC will stay connected - this also means I can use my IRC client from anywhere I like as long as I have SSH access.

So how do I use it?

First of all you'll need to have 'screen' installed on your system - Debian users should be able to 'apt-get install screen', users of other distros will need to find packages for their system (e.g. RPMs) or compile it from source. Once its installed, to start a new screen session, simply execute:

$ screen

You'll see a fullscreen message telling you which version of screen you're using and a short bit of license information - just hit space or Enter to continue. You'll be presented with a shell prompt which will look strangely identical to what you had before, but now this is running inside screen. We can now test that its possible to disconnect and reconnect this screen session by running a simple command:

$ ls

and you should get a normal directory listing. Now press ^a-d (Ctrl-a and then 'd') and you'll exit the screen session and see a small [detached] message displayed to tell you this. We can see that screen is still running by using the list command; here is the output I saw when I ran it:

bluegremlin@ewok:~ $ screen -list
There is a screen on:
        1107.pts-3.ewok        (Detached)
1 Socket in /var/run/screen/S-bluegremlin.

bluegremlin@ewok:~ $


This shows me that there is one session currently running, and that 1107 is its process ID. I can now retrieve it by typing:

$ screen -r

If there were several screen sessions running then you would have to specify which one you want to retrieve, and you do this by specifying enough of the process ID to make it unique. For example if there were two sessions running, one with a PID of 1107 and one with a PID of 1120, then typing 'screen -r 110' would retrieve the first, and 'screen -r 112' would retrieve the second. To end a session, simply exit the last running application (this may be a shell) and you'll get a message like is terminating to let you know what's going on.

But this can become tedious if you have lots of screen processes. So screen allows you to name each session at invocation time, hence:

$ screen -RS mutt mutt

Will start a screen session called 'mutt' and will run the command 'mutt'.

If you want to start it detached (useful for running it on startup), you can use the -d and -m options:

$ screen -dmS mutt mutt

[[n6tadam@station|tmp]]$ screen -ls
There are screens on:
        23523.mutt      (Attached)
        25362.myshell   (Detached)
2 Sockets in /var/run/screen/S-n6tadam.


So that if I wanted to resume the "myshell" screen, I'd use:

$ screen -r myshell

Great! We're now able to start a session, run stuff in it, disconnect it and later reconnect it from the same or a different terminal. This is the most basic way in which to use screen, but there are some other useful features that are worth learning.

Adding new sessions

Screen can manage more than one shell or application per session, and you can create a new one by pressing a-c and then cycle between them with a-n. Its best to just try this out for yourself rather than trying to explain it here, so give it a go!

Another way of starting screen is to specify the application you want to run in it so that it is spawned immediately instead of a shell. For example, you can run:

$ screen make modules

and this will start a 'make modules' in a new session. Importantly, though, when the 'make' command terminates (for whatever reason) the screen session will also terminate and it will not be possible to retrieve it. Put simply, if you want to be able to look at the final output of a command you run inside screen, don't run it like this.

Viewing more than one screen

Screen supports a split view to allow you to view more than one "screen" at a time. This means you can keep an eye on the progress of one command whilst still checking up on the MailingList or chatting in the IrcChannel. Start a screen session as shown above. Once inside the session, hit a-S. (The capital "S" is very important - using a lower case "s" sends a Ctrl-s signal to the process and you end up with a locked up screen session! If you ''do'' hit a-s by accident, a-q will unlock your session.) Once you've hit a-S, you will see the contents of the screen session resized to take up the top half of the terminal, and the bottom half becomes blank. Hit a-Tab to switch the focus to the bottom panel and then a-c to start a new screen session inside that bottom box. You can switch back and forth between the two panels using ^a-Tab.

It's worth noting that split regions within screen do NOT persist if the screen session becomes detached or is locked. This is because internally, screen is not aware of the different regions at all. That's not to say that the persistent regions cannot be faked -- the trick it to start another screen session inside the split region.

Copy and pasting text

Screen also supports the ability to copy and paste text from within the same "screen". To change into cut&paste mode use ^a-[[ you|can then move the cursor around the screen using the arrow keys (you can go back through text that has scrolled off the top of the screen simply by pressing up untill you reach it).

When you are positioned at the start of the text you want to copy hit return, now when you move the cursor it will mark text in inverse video (white text on a black background), this is the text you will copy. Once you have selected the text you want hit return again to stop copying and move to the position you wish to paste to and hit ^a-]] the text will magically appear.

To abort copy mode simply hit ^a-] without selecting any text (this is usefull i fthe output of your last command has scrolled off the top of the page to fast for you to read)

Sharing screens

You can connect to a screen session multiple times. This allows you to use the same screen session on multiple terminals (potentially in differing locations). Connect to one screen session as standard, and then use screen -x to connect a second terminal to the same session. You can then see the same output on both terminals. N.B. You need to use the same username for both screen sessions for this to work.

LinuxHints/Screen (last edited 2006-11-25 19:34:16 by 80)