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

GNU Screen

The key to being an effective administrator is understanding the software that you administer. Software administrators are nothing more than employees who are well rounded in software applications, their versions, how they are configured and installed and lastly how to troubleshoot them should something go wrong. Effective administrators also have a set of tools under their belt that helps make their job easier. The tool that is the subject of this post is GNU screen.

First, we need to get a good grasp of screen. Screen is what we call a "terminal multiplexer". A user attaches himself to a console on the machine, then at that users whim, detach from the console, but still leave it installed and its attached processes running on the system. What makes screen so attractive is the ability to reattach to that console, with its currently running processes available. Generally, screen is run on a remote SSH server, so the administrator can log into that box from anywhere, reattach his running screen session, and continue working on his applications like he never left the machine.

Heres how it works. Log into your local machine, and type:

$ who am i
aaron    pts/1        2008-10-12 05:43 (:0.0)

This shows me logged in as aaron, and attached to pseudo terminal 1. I logged into the system on October 12, 2008 at 5:43 in the morning (my daughter won't sleep). Now, let's execute screen, and run a couple of commands:

$ screen
$ who am i
aaron    pts/4        2008-10-12 05:45 (:0:S.1)
$ cat /dev/zero > /dev/null &

Still logged in as aaron, but this time, I'm attached to pseudo terminal 4. Also, we ran a pretty worthless command, however, it will illustrate the point. Detach from your screen session by typing the following keyboard sequence:

ctrl-a d

This will "detach" you from pseudo terminal 4, but still leave the device created, will all running processes attached to it:

$ ls /dev/pts/4
$ ps -ef | grep cat
aaron    27684 27494 93 05:49 pts/4    00:00:05 cat /dev/zero

The process that we ran in screen a moment ago is still running. In my case, it has a process ID of 27684 with a parent process ID of 27494 and attached to pseudo terminal 4, just like we would expect. I wonder what process ID 27494 is...

$ pstree -p | grep -B 1 27494
        |               `-zsh(27494)---cat(27684)

(The 'pstree' command, if run on its own, will show your the processes running on your box in a tree-like structure, showing parent and child processes).

As we can clearly see, 'screen' is process ID 27492, with a child process of ZSH, my favorite shell, running as process ID 27494. ZSH is currently running our 'cat' command as process ID 27684, as we just discovered.

Here's what is absolutely great about GNU screen: I can detach from sessions and reattach to those sessions at will. Suppose I ran that command on my box at home. When I get to work, I want to keep an eye on it. All I need to do now, is login remotely to my machine (hopefully, it's running an SSH server), and reattach my screen session:

% ssh
$ who am i
aaron    pts/0        2008-10-12 06:00 (
$ screen -r
$ who am i
aaron    pts/4        2008-10-12 06:01 (:0:S.1)
$ kill 27494
[1]  + terminated  cat /dev/zero > /dev/null
$ exit
[screen is terminating]

Not only can we detach and attach ourselves from and to these processes respectfully, we can also create new terminals in screen, split our screen horizontally, add multiuser sessions, lock the screen from intrusion, and many other features. The capabilities of screen go well beyond what we've discussed here. Suffice it to say that GNU screen is one of the most powerful tools that a system administrator can have in his tool belt.

GNU screen is available in the Fedora, openSUSE and Ubuntu repositories, as well as Red Hat Enterprise Linux and SUSE Linux Enterprise Server and Desktop.

{ 4 } Comments