Using Screen for Session Management

terminal-icon_128x128Lost your shell connection? Need multiple shell sessions?
You are logged into your remote server via SSH and happily plucking along at your keyboard and then it happens. Suddenly, the characters stop moving and then you get the dreaded “Connection Closed” message. You have just lost your session. You were halfway through some task and now you have to start over. Ugh. Well you can prevent this from happening by using screen. Screen can not only save you from disconnection disasters, but it also can increase your productivity by using multiple windows within one SSH session. Use Screen for Session Management!

What is Screen?
As the man page states, “Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells).” This can be a life saver when working on your dedicated server. Screen has a several great features for helping you administer your server more productively and safely. I am going to discuss the three features (multiple windows, logging, sessions) that I use the most but be sure to see the man page for full details.

$ sudo apt-get install screen

Using Screen
Screen is started from the command line just like any other command:

$ screen


You may or may not get a text message about screen. If you do not, then you probably think nothing has happened, but it has. You are now inside of a window within screen. This functions just like a normal shell except for a few special characters. Screen uses the command “Ctrl-A” as a signal to send commands to screen instead of the shell. To get help, just use “Ctrl-A” then “?”. You should now have the screen help page.

Key bindings are the commands the screen accepts after you hit “Ctrl-A”. You can reconfigure these keys to your liking using a .screenrc file, but I just use the defaults.

Multiple Windows
Screen, like many windows managers, can support multiple windows. This is very useful for doing many tasks at the same time without opening new sessions. As a systems manager, I often have four or five SSH sessions going at the same time. In each of the shell, I may be running two or three tasks. Without screen, that would require 15 SSH sessions, logins, windows, etc. With screen, each system gets its own single session and I use screen to manage different tasks on that system.

To open a new window, you just use “Ctrl-A” “c”. This will create a new window for you with your default prompt. For example, I can be running top and then open a new window to do other things. Top stays running! It is still there. To try this for yourself, start up screen and then run top.

tymonn@debianbox:~$ top
top - 15:40:41 up  1:28,  3 users,  load average: 0.25, 0.36, 0.36
Tasks: 120 total,   2 running, 116 sleeping,   2 stopped,   0 zombie
Cpu(s):  5.2%us,  1.1%sy,  0.0%ni, 93.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   2075424k total,  2033216k used,    42208k free,   571872k buffers
Swap:  1052184k total,        0k used,  1052184k free,   969644k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 3624 tymonn    20   0  148m  59m  20m S    3  3.0   3:35.83 rhythmbox
 5073 tymonn    20   0  260m 117m  24m R    3  5.8   9:46.60 firefox-bin
 3261 root      20   0  325m  91m  22m S    2  4.5   1:24.69 Xorg

Now create another window use “Ctrl-A” “c”

$

Now you have 2 windows, You can create several windows and toggle through them with “Ctrl-A” “n” for the next window or “Ctrl-A” “p” for the previous window. Each process will keep running while your work elsewhere.

Leaving Screen
There are two ways to get out of screen. The first is just like logging out of a shell. You kill the window with “Ctrl-A” “K” or “exit” will work on some systems. This will kill the current windows. If you have other windows, you will drop into one of those. If this is the last window, then you will exit screen.

The second way to leave screen is to detach from a windows. This method leaves the process running and simple closes the window. If you have really long processes, you need to close your SSH program, you can detach from the window using “Ctrl-A” “d”. This will drop you into your shell. All screen windows are still there and you can re-attach to them later.

Attaching to Sessions
So you are using screen now and compiling that program. It is taking forever and suddenly your connection drops. Don’t worry screen will keep the compilation going. Login to your system and use the screen listing tool to see what sessions are running:

tymonn@debianbox:~$ screen -ls
There are screens on:
	6039.pts-4.debianbox	(14/06/2008 15:02:09)	(Detached)
	5873.pts-0.debianbox	(14/06/2008 14:56:33)	(Detached)
2 Sockets in /var/run/screen/S-tymonn.

tymonn@debianbox:~$

Here you see I have two different screen sessions. To re-attach to a session, use the re-attach command:

tymonn@debianbox:~$ screen -r 6039.pts-4.debianbox

Just use screen with the -r flag and the session name. You are now re-attached to the screen. A nice thing about this, is you can re-attach from anywhere. If you are at work or a clients office, you can use screen to start a job and then logout. When you get back to your office or home, you can login and get back to work.

Screen Logging
As a consultant, I find it important to keep track of what I do to someone’s server. Fortunately, screen makes this easy. Using “Ctrl-A” “H”, creates a running log of the session. Screen will keep appending data to the file through multiple sessions. Using the log function is very useful for capturing what you have done, especially if you are making a lot of changes. If something goes awry, you can look back through your logs.

Leave a comment