Using Screen
Steven G. Isaacson
Although the user's perspective of modern computing is usually graphical, much of what systems administrators do is in character mode, perhaps through an xterm window, a telnet session, or an actual ASCII terminal attached as the system console. Often, we dial into the network or a system configured as a communications server to perform administrative tasks during what other people consider non-business hours. Such single-threaded connections, however, leave much to be desired from a convenience standpoint. One solution to the limitations of such connections is a utility called "screen."
Screen is a window-managing program that allows you to interact with one or more programs (usually login shells) from a single session. You can cut-and-paste between windows, view scroll-back history, log activity, and lock the screen -- all from a single telnet or rlogin session. The most remarkable feature of screen, however, is its ability to detach and reattach sessions.
Detachment Detaching a login session is analogous to putting a phone call on hold, except that you can pick up the call again from any phone in the world. For example, suppose you're at your desk working with two telnet windows open. In one window, you're replying to email and in the other, you're checking the configuration on some remote system. Then, the phone rings and you're called to the computer room. In the computer room, you have to reboot a computer. While it's rebooting, you grab another terminal, log in, and reattach to your email and remote configuration telnet sessions. Both sessions appear exactly as they did when you put them on "hold" at your desk. You're now merely continuing the conversation from a different terminal.
A beneficial side effect of using screen is that you're automatically immune to otherwise catastrophic events. For example, if your phone line disconnects, or the network crashes, or someone kicks out the plug to your terminal, you simply dial or log back in and reattach. Or, more routinely, when it's time to go home from work, you tell screen to detach, then turn off your computer or monitor. You don't have to save files and exit applications. You don't have to do anything except leave. When it's time to start work again, you log back in and voila, everything is right where you left it.
Why is this ability to detach so important? Consider briefly the evolution of a systems administrator's productivity. First, you learn that commands may be terminated by a semicolon as well as a new-line:
cd ; pwd
Then you learn that programs may be run in the background:
myprog &
Output can be saved to a file:
myprog > output &
Eventually you learn that even when you logout and go home, your program may still need to be running. You do this by telling your program to ignore the hangup signal from your terminal when you logout:
nohup myprog > output &
Later you can log back in and check the progress of myprog:
tail -f output
But, all of this assumes that 1) you've got a controlling terminal or 2) you can get rid of the controlling terminal. What if you want to disconnect from your controlling terminal (detach) and then connect again (reattach)? You can't unless you have a program like screen. This is a big problem if the program you started running in the background suddenly prompts for input. If there is no controlling terminal, there is also no keyboard and generally no way to supply the needed input. So, the program stops.
If you decide not to run programs in the background, then the terminal is in use (and unavailable for anything else) until your program has completed. Assuming you have terminals to spare (if you're not able to reattach sessions), then you must be in front of the terminal to see what's on the screen. You can't, for example, log in from home and take a look. It's possible to log the entire session with tee, but you still can't press "Enter" unless you're at the keyboard. You need a virtual terminal that can be attached, detached, and reattached.
Screen is particularly valuable for remote installations. From my desk in Seattle, I log into a computer in Connecticut. I can begin installing software and then detach when it reaches the "hands free" stage. Later, I can reattach to make sure things are still running smoothly. I can reattach from my desk, from the computer room, or from home.
Multiplexing Screen can also multiplex, that is, it can work with more than one screen at a time. This ability is not new to SCO users familiar with the console's virtual terminals, nor is it new to HP-UX users fortunate enough to have stumbled upon their Terminal Session Manager (tsm(1)). From any SCO console, or any HP-UX terminal, you can, for example, start a compile in one window, check mail in another, and begin an ftp session in still another.
This ability to run several login sessions from a single connection is invaluable, although no longer remarkable. These days, you usually open a new window on an X terminal or PC. But what if you're dialing into the company computer from home and you only have one phone line, or you're logging in from a console that only supports one session? With a multiplexing program like screen you can now work as if you had 10 terminals in front of you.
Operation To start the program, type screen. Screen then creates a single window with a shell in it. You now have the ability to detach and reattach, turn output logging on, etc. But mostly screen stays out of your way. You work as you normally would.
<ctrl>-a is the default key used to send a command to the screen window manager. To create a new window, type <ctrl>-a, and then c. Now you have two windows.
To switch from window to window you can type <ctrl>-a n, to go to the next window, <ctrl>-a p, to go to the previous window, or <ctrl>-a and the window number. For example, <ctrl>-a 7 would switch you to screen number 7. How do you detach? <ctrl>-a d, to detach. If you're viewing screen number 7 when you detach, screen number 7 will reappear when you reattach. To reattach the next time you log in, start screen with the -R flag (see the sidebar of "Frequently Used Commands").
Installation The current release of screen (it's free) is screen-3.9.2.tar.gz, available from ftp.uni-erlangen.de:
http://ftp.uni-erlangen.de/pub/utilities/screen/
Installation is usually as easy as extracting the code and then:
./configure ; make
Bug reports, enhancements, etc., may be sent to screen@uni-erlangen.de.
The screen man page (which accompanies the source) is almost 50 pages long. It has more information on scroll-back history, locking the screen, dealing with kanji characters, cutting and pasting between windows, and more.
With screen, I can now pick up where I left off from anywhere in the building or from anywhere accessible to our network. It makes being more productive almost too easy.
About the Author
Steven G. Isaacson currently works at HK Systems (http://www.hksystems.com). He may be reached via email at: steven.isaacson@hksystems.com. |