After you use it, you wonder how anyone can live without it and post to your favorite mailing list or forum, agog with the wonders of your new discovery.
Screen is one of those programs. I cannot count the times I've seen an excited post about it. It's not a glamorous application. It is, as its manpage says, "...a full-screen window manager that multiplexes a physical terminal between several processes." Ok, that doesn't sound too exciting, but it is one of the most useful applications you will find, especially in remote administration.
Most distributions, if they don't install it by default have it available as a package. If not, it can be gotten from Gnu's website. Then, it can be compiled and installed. Assuming you are going to accept all defaults, after downloading the package (version 4.0.2 at time of writing--your version may have a different number)
tar -zxvf screen-4.0.2.tar.gz cd screen-4.0.2 ./configure && make make install
Note that the make install portion must be done as root or with root privilege.
Now that screen is installed, it's time to investigate its usefulness. To start screen, at a command prompt type
You'll see a note about it, and a message to Press Space or Return to end. Do so.
Now it seems as if you're at an ordinary console (or your xterminal of choice) looking at your usual command prompt. Suppose you are in console. For our first example, we'll do something very simple.
You are in your $HOME directory. In there, you have a doc directory and you want to check your docs. Change into your doc directory and do ls.
Unsurprisingly, you see a list of your docs. Now, you need to do a ls of the directory above it, your $HOME directory.
The default prefix for a command while in screen is Ctl+a. For the rest of this article, it will be abbreviated C-a. So if you see C-a in any of the examples (and you will see many of them) don't type an upper case letter C, hit the Ctl key
You are back at a blank console, in your $HOME directory. The C-a was the prefix to a command and the c is for "command". Now, you can do another ls, and have a list of the files in your $HOME directory. To go back to the first screen, hit
You are back on the first screen, the one that is showing a list of files in your doc directory. You can go back to the second screen with
Most of screen's command keys are straightforward. The C-a prefixes all commands, the c was for command or command prompt, the p was for previous and the n for next.
So far, this isn't terribly impressive. One can do almost the same thing with alt+F2, alt+F1, switching between virtual consoles. One could argue that screen's keystrokes are easier for a touch typist. This is even more true in NetBSD and OpenBSD where one switches between virtual consoles with Ctl+Alt+F2. However, this becomes far more useful when you are logged into a remote machine using putty or another ssh client. By using screen, you can work with several virtual consoles on the remote machine with one putty window.
One of screen's best features for those who have to remotely adminster machines is its detach function.
You have to log into a remote machine. After logging in, you have to tar and bzip an enormous directory, an operation which will take 45 minutes. Your connection is not the best, and you worry about being disconnected before the operation is complete. However, the remote machine has screen installed.
After logging into the remote machine, your first command is
Begin the tarring and bzipping.
tar -cf enormous_directory.tar && bzip2 enormous_directory.tar
Now detach the screen
The C-a as we know is the command prefix. The d is for detach. You will see a message reading
and be back at a command prompt. Note that this command prompt is not in screen, it is your normal command prompt.
You can log off and log back in 45 minutes later. After you log in, type
The -r means reattach and will put you back in the screen where you have been tarring and bzipping the large directory. If it is done, you can type exit, and you will see a message that screen is terminating.
If you have several things to do on the remote machine, you might lose track of which screen is doing what. When you log back in you can get a list of your screens with
This returns a list of screens. You might see something like
There are screens on: 6795.ttyp1 (Detached) 26270.ttyp3.server (Detached) 26315.ttyp5.server (Detached) 3 Sockets in /tmp/screens/S-username
The trouble with this is remembering which screen is doing what. The first number is the PID. So, if you remember that you used the first screen to tar and bzip a large directory, the second screen to copy some large files and the third screen to do something else, then it's easy. You can type
and be back at the screen that is tarring and bzipping a file.
If you are going to run several screens, a better way to do this is to start each session with a descriptive name. Say one session will be used for the tar and bzip, another to copy a directory of data and the third session is to copy a large directory of images. Start each session with the -S (as in session name) option and give it a descriptive name. In this case, we'll call them tarbzip, cpdata and cpimage.
Begin the operation, and then detach the screen with C-a d. Now, do the same thing for the other two screens, starting one with screen -S cpdata and the other with screen -S cpimage. Now, if you log back in and want to check your list of screens, doing screen -ls will show something like
6795.tarbzip(Detached) 26270.cpdata (Detached) 26315.cpimage(Detached) 3 Sockets in /tmp/screens/S-username
So, as the cpdata screen is probably done already, you first check that one with screen -r cpdata. It makes it much easier to keep track of your screens.
There are plenty of other uses for the detach and reattach feature whether or not you do remote administration.
If you're like me, frequently switching between X and the console, it's handy for starting anything that might take a bit of time, whether it be compiling a large program or chatting on irc with irssi or another client that can be used in console. You can begin a screen session while in console, detach it, start X, open an xterm and reattach the session. Suppose, after logging in, I decided to fetch a large file using ncftp, wget or some other program. Now, I decide I want to do some web browsing with firefox. I can detach the screen session where I've begun the download, fire up X and do my browsing. If I want to check on the download's progress, I can open an xterm, type screen -r and see how it's doing.
This is the sort of handy little thing that makes screen such a convenient program. Are there other ways to do that? Of course. In Unix (and by inference, Linux, the BSDs and any other Unixlike system) there's always more than one way to do something. (The corollary is that someone is sure to think your way is completely wrong.) I could start the download in console, then hit Alt+F2 to open another console. In that console, I could start X
Then, I could hit Ctl+Alt+F1 to return to console. It would bring me to the screen where I started X. I would hit Alt+F1 to bring me back to the console where I was doing the download, check on it, then hit Ctl+Alt+F7 (F9 in FreeBSD) to go back to X. I think using screen is easier.
Screen can also open multiple windows in one console. The usefulness of this is obvious. I'll sometimes use it when installing a system where I haven't yet installed X, or don't plan to install X. Sometimes, I forget syntax and want to check the man page, or perhaps, I have a text based browser installed and want to check a particular web page to remind myself how to do something. With screen, this becomes simple. Rather than having to switch back and forth between consoles, hoping I don't forget what was in the second console, I can split the console window. Then, I can work in the top window and in the bottom one, I can open up w3m (my text based web browser of choice) and go to the web page I need, or perhaps ssh into another machine with a configuration file I want to examine. To do this, open a screen session. Then
The console window will now be split into a top and bottom window. (The S stands for "split".) Your cursor will be in the top window. To move to the bottom window, use C-a and then hit the Tab key. The cursor will now be in the bottom window. Then, C-a and c for command, and you can type in that second window. To move back and forth between the two windows, use C-a and Tab.
You can actually use more than two windows if you wish. While in either of the two windows once again do C-a Shift+S and you will now have three windows. Do it again, and you'll have four windows, but of course, even with a large monitor none of the windows will have more than a few rows. In practice, I seldom use more than two windows in console.
Sometimes, while trying to tab between windows, you'll accidentally hit C-a n or C-a p and suddenly find that the two windows are identical. If this happens, just hit C-a p or C-a n again and you'll find yourself back where you wanted to be. Obvious though this may seem, it can get the newcomer--suddenly, they find themselves with two identical windows on their split screen and not realizing why it happened, they don't know how to get out of it.
To end a split window screen session, simply close them one at a time by typing exit. When you've typed exit in the last window, you'll get the usual message that screen is terminating.
For a program that doesn't seem to do very much, screen is an extremely handy utility. This article only gives the most basic uses of screen. Take a look at the screen man page. You'll see some of the other things it can do.