Improve Your Computing Experience: Using screen

Description of screen

I am continuously looking for ways to improve my computing experience. I tend to try out new programs I hear of, and make them part of my everyday routine if they are of good quality.

While reading BSD Hacks (ISBN 0596006799, O'Reilly Media, Incorporated), I discovered the Unix utility "screen". I have since heavily integrated screen into my everyday computing environment.

From the screen man page: "Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells)." In other words, screen allows you to run several terminals at once from one terminal. This allows you to have many running commands without having to maintain many terminals.

A Beginner's Guide to screen

It seems as though it will be easiest to describe the various features of screen by showing examples. To start screen, simply type screen at the terminal. You will now be presented with a new terminal. You now have two ways to get back to your original terminal. You can type exit at the prompt, which ends screen, or you can "detach" your screen session. This has the nice effect of keeping any processes that you have currently running under screen running. To "detach" from the screen session, simply type "ctrl-a d". That is, hold down control and a at the same time, let go, and then type d.

You can start screen with some switches to reattach a detached session. Running "screen -dr" will reattach a detached session. For example, let's see how we can use screen to start a bit torrent download, keep it going, and then come back to it later (lines in blue are meant to denote transitions on the terminal):

stu@pita|/data1 11:54pm> screen
User starts screen
stu@pita|/data1 11:53pm> btdownloadcurses.py 5.4-RELEASE-i386-bootonly.torrent
User starts bit torrent
 ------------------------------------------------------------------------------
| file:     5.4-RELEASE-i386-bootonly                                          |
| size:     22,003,783 (21 MiB)                                                |
| dest:     /data1/5.4-RELEASE-i386-bootonly                                   |
| progress: #######___________________________________________________________ |
| status:   finishing in 0:01:28 (10.7%)                                       |
| dl speed: 127.8 KB/s                                                         |
| ul speed: 0.0 KB/s                                                           |
| sharing:  0.000  (0.0 MB up / 1.3 MB down)                                   |
| seeds:    4 seen now, plus 0 distributed copies (1:9.5%, 2:0.0%)             |
| peers:    0 seen now                                                         |
|                                                                              |
 ------------------------------------------------------------------------------
User detaches screen with "ctrl-a d"
[detached]
stu@pita|/data1 11:57pm> screen -dr
User reattaches the screen session
 ------------------------------------------------------------------------------
| file:     5.4-RELEASE-i386-bootonly                                          |
| size:     22,003,783 (21 MiB)                                                |
| dest:     /data1/5.4-RELEASE-i386-bootonly                                   |
| progress: ################################################################## |
| status:   seeding (100.0%)                                                   |
| dl speed: 0.0 KB/s                                                           |
| ul speed: 0.0 KB/s                                                           |
| sharing:  0.000  (0.0 MB up / 20.0 MB down)                                  |
| seeds:    0 distributed copies (next: 1:0.0%)                                |
| peers:    0 seen now                                                         |
|                                                                              |
 ------------------------------------------------------------------------------

As you can see, this is a good way to start a program, and come back to it later. Screen, as was described by the man page, can support multiple terminals at once. I guess you could think of it as having a bunch of LCD monitors on your desk and flipping through them. You create a new terminal within screen by typing "ctrl-a c", and scroll (forward) between the terminals with "ctrl-a n". screen will only exit after exiting all terminals within screen.

You will notice that if you start multiple instances of screen, and then try to attach to one with "screen -dr", screen will not know which one to attach to. At this point you have to refer to the screen by name. Screens are named automatically unless specified. I always specify a name, to make it easier on myself, by typing the command "screen -S name". You can view the available screens by typing "screen -ls". For example:

stu@pita|/data1 12:12am> screen -dr
There are several suitable screens on:
        80337.dev       (Detached)
        44873.im        (Detached)
        56010.bt        (Detached)
        56180.ttyp0.pita        (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.

You need only specify the part after the number as the name of the screen using "screen -dr name". As you may have guessed, the fourth entry in the above list is what a screen name will look like when you don't specify -S when starting a new screen.

At this point in my little tutorial, you are ready to start using screen effectively. Keep in mind that when you detach a screen, the programs that you had running when you detached will still be running when you reattach, even if you kill the ssh connection that you may have been using to access the machine. When you resume screen, you can be on a regular local console terminal even if you started screen over ssh. Of course, screen will die with a reboot, but you can't ask too much.

Use screen as an Multi-User Terminal "Conference"

One switch I discovered while looking through the man page was the -x switch. This allows you to connect to a screen session which is not detached. This is not the same as the -d switch, because -d detaches the screen before reattaching to it. So this means that with -x, more than one person can attach to the same screen session at the same time. I would imagine that this feature would be great for having someone walk you through some set of commands that you are unfamiliar with, from a remote location. I also think it would be great for a class. Of course, all users can also enter input to screen when using this switch, and this may take some of the usefulness away from the -x switch. And of course you and any user that would like to attach to the same screen session will have to log in to the machine using the same username and password (as far as I can tell). So this feature seems extremely useful, as long as you trust the other users that are attaching to your screen session.

Making screen Easier to Use for People that Use screen a Lot

After I started using screen a lot, I found myself making screen more integrated into my environment, and easier to use. First of all, since I found myself almost always attaching to screen every time I logged on to one of my machines, I put the following line in my .cshrc file: "screen -ls". This simply displays the available screens upon login. Also, I sometimes find myself running a command and then wishing I had started screen beforehand. Having screen -ls run every time I log in reminds me to start a new screen if I don't want to attach to one that is already running. Also, since I often run screen with different switches, I have created aliases in my .cshrc file:

alias scdr      screen -dr
alias scls      screen -ls
alias scx       screen -x

A Note on the Usefulness of this Tutorial

Please be aware that this tutorial is a description of how to run screen in a way that I find useful. There are many more features that I have not gone into (including screen splitting). As always, refer to the man page. While in screen , you can get help by typing "ctrl-a ?".

Further Reading

screen man page
screen home page (www.gnu.org)
GNU Screen: an introduction and beginner's tutorial (www.kuro5hin.org)

This document was written by Stuart Matthews on 2005-06-30 and is covered under the GNU Free Documentation License which can be read in full at http://www.gnu.org/copyleft/fdl.html. Stuart Matthews can be contacted at stu@fourmajor.com