Take console productivity to a new level with Screen


Author: JT Smith

Screen is an application that’s often underestimated. Screen is, simply put, a screen manager with VT100/ANSI terminal emulation. Think of it as a full screen, text-based window manager for your terminal or console. For what it is, it’s an incredibly feature-rich application. In this article, I will explain what it does and why it’s so useful.
According to the man page, Screen “multiplexes a physical terminal between several processes (typically interactive shells).” What this means to the rest of us is that you start up Screen and run something in it (typically with a shell), and do whatever you normally would in a terminal. This is all fine and dandy but in and of itself isn’t anything new.

Perhaps the most popular feature of Screen is something called detaching. Let’s start with an example. It’s 4 p.m. and you need to start compiling an application that takes around three hours to compile. After it compiles, you need to reboot the system. All this needs to be done by tomorrow morning. Typically that would mean spending the evening at the office, but not with Screen. With Screen you can start it up, start compiling the application, and then detach from the screen session altogether. Detaching will take you back to the command line from which you originally ran screen. At that point you can actually log out of the system and go home for dinner.

Before you panic, let me remind you that you did not suspend the compile job or forfeit your rights to the processor. It’s still compiling. You just don’t have to keep that specific terminal open in order to keep tabs on it. After you’ve enjoyed a nice dinner at home, you open up your laptop and SSH (Secure Shell) into the system, re-attach your Screen session and see what happened with the compilation. You’ll be at the same command prompt where you started the compilation, complete with a buffer of what happened. You then simply reboot the system remotely and you’re ready for the meeting in the morning — and it didn’t take all evening.

To start Screen, type: screen. That starts Screen and runs an instance of your interactive shell (usually Bash). You can do whatever you want in there, just as if it was just another one of your xterms. By default, each command to Screen begins with a C-a (Control-a), and is followed by at least one other keystroke. For example, when you’re ready to detach the session you hit C-a, followed by d. When you decide to re-attach the session, you will type something like screen -r with optional arguments including the sessionowner, pid, tty, and/or host.

Now that you’re convinced that Screen is useful, let’s talk about being productive with it. The Screen feature that as been the most useful to me is the windowing capability. Screen allows you to have multiple windows in one session. You can have one window that’s compiling something on one machine, another that’s SSHed into another server editing a configuration file, and yet another monitoring a log server. Already do this with just a group of xterms on your screen? Let’s go through an example where Screen really shines.

My name is Joe LinuxAdmin and I administer a cluster of six Linux servers doing various tasks, ranging from Web serving to file and printer sharing. What I need is a portable system administration environment. I’ve decided to use Screen. I start my session by typing screen. I now have something that I can remotely detach and re-attach, so that’s good. I’d like to have an open shell into each of the servers I administer, so I’ll create a window for each one and log in to each server. (To create a new window, I type C-a followed by n.) I can do this as many times as I want, so it’s easy to create the six windows I’ll need to access all of my servers at once.

After I’ve created my windows, I need to navigate them. To go to window number four, I’ll type C-a 4. Instantly I’m viewing window number four. If I want to switch to the next window, I type C-a n. Now that I’ve got a server with the hostname of zeus on window 5, how am I going to remember that zeus is on 5? I can always hit C-a A and type in zeus. After I name my windows, I don’t need to cycle through them. I can just as easily type C-a ‘, then type in the name of the machine I want — and there it is. If I’m especially conservative with my keystrokes, then C-a “ will show me a list of my windows along with their titles.

I now have, in one terminal, windows open to all of the servers I administer. Everything is right at my fingertips. I’ve got it all set up. It’s now 5 p.m. and time to go home. Around 8 p.m. I’m out for a walk with my date. Lo and behold, my pager goes off. As usual I look for the nearest computer to address the situation and end up in a cybercafe on a Windows machine. Having a free SSH client on the windows machine is easy thanks to PuTTY, but it’s going to take me forever to load up all of those terminals and login to all of the machines. It’s too bad I left my Screen session attached at the office.

But wait! This is where Screen excels. I log in to my machine and type screen -dr. It remotely detaches my session and re-attaches it where I am. I now have my Screen session with all of my windows, all of my servers, and my entire administration environment right in the cafe. None of it is dependent on the client I’m using. I could be on a Linux machine, a Windows machine, or a Mac OS X machine.

Having windows for different servers you’re logged into is just one example of the uses for multiple windows within Screen. It works equally well for a development environment. You can have one window for a text editor, another for compiling, another for debugging, and yet another running your application. It’s a mobile environment that can be accessed from anywhere.

Screen has many powerful features that go beyond attaching and detaching. For example, you can have more than one person at a time attached to a screen session. Real-time editing by two people on one file — how’s that for collaboration? You can also password-protected Screen sessions to add a level of security. In addition, you can have more than one Screen session per user. The possibilities are endless.

Next time you’re looking for a level of flexibility at the command line that you feel just isn’t possible, I recommend reading up on Screen. It’s a powerful utility that will enhance your productivity — and at the same time it will make your life easier.