Sunday, December 21, 2008

Screen for Beginners

Perhaps I got spoiled at the last couple of companies I was at, but I was shocked when I discovered that my new coworkers had never heard of screen, much less used it. In the past couple of years, I have found screen to be such a valuable program that I can't imagine life without it. I use it on a regular basis both at work and at home, partly because it's so handy and partly because I'm a nerd.

There are plenty of ways to use screen, but there are three things that I like to use it for:

* Maintaining a perisistent shell on one computer that I can access from multiple computers.
* Maintaining a perisistent development environment.
* Logging into somebody else's shell to monitor them, or demonstrate techniques to them.

Screen doesn't ship installed on every distribution, but it's easy to get installed:

RHEL/Fedora: yum install screen
Ubuntu: apt-get install screen

Once it's installed, it's easy to get running. Just type "screen" from the command prompt. The display will blank, and you'll be left at a prompt that looks just like the one you were just at. If you're doing thing in gnome-terminal, you might see something like [screen 0: bash] in your title bar.

At this point, you can use the command line exactly as you did before; you're really just running another shell. Go ahead and run top, to see what I mean. But now you have a series of commands available, each of which starts with CTRL-a. The first one you'll want to be familiar with is CTRL-a d (that's a CTRL-a followed by a d, not CTRL-a CTRL-d). This will detach from your screen session. When you do this, you'll find yourself at the command prompt that you were at before, with "[detached]" displayed above the prompt.

At this point, screen is running, but it's in the background. If you didn't exit out of top, then it is still actively running, you just can't see it. Go ahead and open a second terminal window and type in screen -r. This will reattach to the screen session, and you will see top still running.

That's enough to get you going for a basic persistent shell. I know a lot of people that do this on a server with a public IP address, to keep programs like Irssi open so that they can log into it from work or from home, without having to actually log out of their chat accounts. But let's take this a little further.

You should still have top running. Keep it open and type in CTRL-a c. This will create a second screen, with its own command prompt. Go ahead and type in free.

Now that you have two screens going, type CTRL-a p. This will switch to the previous screen, where top is running. Type CTRL-a n to go to the next screen, where free is running. You could also use CTRL-a 0 and CTRL-a 1 to go to a specific screen number. If you hit CTRL-a c again, you'll get yet another screen.

I've found this to be useful in maintaining a persistent development environment, so that I don't have to leave my notebook at work or close several instances of vim and bash at the end of the day. But let's look at one more feature of screen.

If your terminal is maximized, go ahead and resize it so that it only takes up half the screen. Hit CTRL-a 0 to make sure we're at the screen with top. No go ahead and open up another instance of gnome-terminal and maximize it. Type in screen -x at the prompt. You will see the top screen running, but it won't be taking up the whole window. That's because you've attached to a screen session that was already running somewhere else, and screen is using the display variables from that other screen.

Those of you who have ever tried to walk another user through something on the phone know what a pain it can be to not know exactly what the user is looking at. This can be especially troublesome when a command prompt in Linux is involved. Isn't it much easier to log into their machine, connect to their terminal, and then watch them work?

Now for some cleanup. Hit CTRL-a d to detach from this screen. Go back to the other terminal window where top is still running and hit q to quit. Type exit at the prompt to close that shell. Continue exiting out of screens, using the exit command. When you've exited out of the last command shell, you will be dropped back at your original shell, with the message "[screen is terminating]" above the prompt.

That should be plenty to get you running for now. When you're ready to get into some of the more advanced features of screen, go ahead and run man screen to take a look at the other options available.

No comments:

Post a Comment

Comments for posts over 14 days are moderated

Note: Only a member of this blog may post a comment.