Image of the glider from the Game of Life by John Conway
Skip to content

Using Screen Effectively

One of the biggest tools that I use is screen. Not only for irssi, my preferred IRC client, but for other things as well. When I show people screen, it's funny to see the look on their face. They take a step back, and say "Wow. That's really cool". I hope somehow that I can convey that same excitement in this meager post.

First off, screen is a wonderful tool that allows you to run a program in a terminal inside a special "session window". You can "detach" the window, exit your terminal, go to work, open up a terminal, and "reattach" the "session window", and your terminal status will be in the exact same state as when you left. This is handy for running programs, such as irssi, where you can pick up right where you left off without missing a beat.

To run screen, obviously, you need it installed. Once installed, pull up a terminal, and type:


A message may come up about screen, where to download it and how to file bugs. Then it will take you to your default prompt. It may just take you there anyway without the "about" message. At any event, you are now running a "session window" inside your terminal, and you should be at your normal prompt.

You can have multiple "session windows" running side-by-side, basically acting like "tabs" in gnome-terminal. You can also have nested screen sessions. The complexity of how differing screen sessions can be run won't be covered here, but may be covered later one-by-one. For the time being, we'll just cover single screen sessions.

You can now "see" your screen session by typing 'screen -list' at the prompt. A brief output about the PID and whether or not it is "attached" is printed to your terminal. You could see something similar to the following output:

aaron@hercules:~$ screen -list
There is a screen on:
        23454.pts-1.hercules    (Attached)
1 Socket in /var/run/screen/S-aaron.

Once inside screen, there are a number of keystrokes that control the movement inside of screen. Such as detaching and reattaching your session, moving forward and backward from one session to the other, logging out, clearing the window, and many more. Usually, the keystrokes are in the form Ctrl-a, letter. For example, while in screen, as you currently should be, type "Ctrl-a, d" to detach out of your session, and back to the normal terminal. That is, type and press "Ctrl-a", release, type d, release. Your session is now "detached". To "reattach", type 'screen -dr' at the prompt.

When detaching and reattaching multiple screens, it may be difficult to know exactly what is being "attached" and "detached". To save any confusion, never forget 'screen -list' (or 'screen -ls') to show you what you have running. When "reattaching" a session, it is safest to type 'screen -dr', which says "run screen, but first detach my running session (-d), then reattach it here (-r). This works if you have only one screen session running. If multiple, you will need to provide the PID also. See 'man screen' for further detail.

Ok. Let's see this in working action. As I mentioned, I use irssi as my default, and only IRC client. However, I don't want to disconnect and reconnect to IRC every time I change my location. So, rather, I run irssi inside of screen. So, first I pull up my terminal, and I am greeted with my friendly terminal prompt. I'll call this prompt "Prompt A". From Prompt A, I run 'screen'. I am now met with a new terminal prompt which I'll call "Prompt B". Prompt B is a running session "inside" Prompt A. From here, I run 'irssi' at Prompt B. I connect to my various IRC servers and chat away.

Now, it's time to go to work. I have two options at this point. I can either: 1) close my terminal directly, or 2) detach my screen session, and exit nicely. If I choose to close my terminal directly, I can rest assured that my IRC connections won't change. Irssi is being protected by my screen session, so closing the terminal will have no effect on it. However, I like to exit everything cleanly, so I issue the keystroke 'Ctrl-a, d' to "detach" my screen session. I am now back to Prompt A. From here, I 'exit' to close the terminal, and go to work.

When I arrive at work, I SSH into my box running my screen session. Once SSH'd in, I am met with Prompt A. I want to start chatting in the IRC session that I started at home. So, at the prompt, I type 'screen -dr' to "detach" the screen session (if I just closed the terminal directly) and "reattach" the session to my current location. My irssi application returns to the screen just as I left it when I was home. I can now start chatting away as if I never disconnected from any of the IRC servers.

Take a second for that to sink in. That rocks! I never missed a beat, and to the others in the channels, it's like I've never left. As far as they know, I'm still at home. It just doesn't get much better than that.

That's just using a single screen session. As I mentioned, you can use multiple screen sessions running side-by-side, or nested screen sessions, depending on your current situation and needs. With multiple screen sessions running in concurrence, you don't need a terminal that has all the bloat with tabs, profiles, etc., such as gnome-terminal. You can use RXVT, and have the same effect of tabs just using screen.

Finally, you should learn how to kill a screen session. When the need for a screen session has expired, rather than letting the process run in the background taking precious RAM, you will need to expire the session. To do this, you need to get the PID from your box. Just 'screen -list' to get that PID. The PID will look something like 23454.pts-1.hercules. Now that you have the id, just type at the terminal (you need to be at a prompt):

screen -X -S ID kill

This will kill the session and the process, as well as any application(s) that were running inside it.

{ 2 } Comments