Using GNU Screen

Posted by Steve on Sun 17 Oct 2004 at 19:54

GNU Screen is an often overlooked application which allows you to run programs in a console section, detach from them and then later resume them. They even keep running when you logout.

Part of the reason why the program is often overlooked is because it has a very complicated description. The GNU Screen Homepage describes it as :

"Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. Each virtual terminal provides the functions of the DEC VT100 terminal... [snip]"

Whilst that's a very technically accurate description of the program it doesn't tell most newcomers exactly what the program does.

Essentially GNU Screen is a windowing system for the console.

When you run it inside an xterm, or a remote SSH login, you can create multiple new windows all in the same session, switch between them, or view two or more at once.

In addition to this a screen session is independent of your connection, so you can connect from one location, run things inside a screen session then detach. All the programs keep running and you can later reconnect and resume working right where you left off.

It's the attaching and detaching that makes screen so useful as it allows you to start long running programs, leave them running in the background and later on come back to them. This is very useful for things like console based IRC clients (irssi-text for example).

Lets start with an example which make it easier to understand what's going on.

If you run screen you will see the following text:

Screen version 4.00.02 (FAU) 5-Dec-03 Copyright (c) 1993-2002 Juergen Weigert, Michael Schroeder Copyright (c) 1987 Oliver Laumann This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program (see the file COPYING); if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. Send bugreports, fixes, enhancements, t-shirts, money, beer & pizza to screen@uni-erlangen.de [Press Space or Return to end.]

Press space and you will be presented with what looks like an ordinary shell window. To test this run a command or two, for example:

skx@lappy:~$ ls /tmp gconfd-skx keyring-9hZRfE mapping-skx orbit-skx ssh-MDkRIT1001 skx@lappy:~$

There you can see I've run the command ls /tmp , and you can see the output.

Now we get to see the power of the program. Press Ctrl+A , then d . You will see a message saying that your screen session has been detached, and will be back at your original prompt:

skx@lappy:~$ screen [detached] skx@lappy:~$

So what happened to your shell? Well it's still there! Simply run screen -R and you will be back at the session you d etached from.

This shows you the basics of using the program, you've learnt how to run a copy of screen, run a command inside of it and detach from it ( Ctrl+A d ) then reconnect ( screen -R ).

This allows you to do a lot already, connect to a host from your office via ssh, start up screen and run your favourite text based IRC client. At the end of the day you can detach from it - leaving it stil there to return to from your home machine.

But we've only scratched the surface of what screen can do, remember we said that it could run multiple windows? Lets create another one.

Press Ctrl + a then c (for create). You'll have a new fresh shell session.

Toggle between the two shells you should have by pressing Ctrl + a then Ctrl + a again.

You should see that you're switching between two independent shell sessions. If you press Ctrl + a then " you'll see a list of the windows you have available to switch between.

This window list shows the window number on the left, along with the name of the window. By default the windows will be named after the shell you are running, for example bash .

To rename the current window use Ctrl + a then A .

When it comes to time to close a window you have two choices, you can either type exit in the shell session to close it which will close that window too, or you can use Ctrl +a k to kill the current window.

As well as using one window to switch between you can display two, or more, windows at the same time!

Use Ctrl + a and S (Capital 's' for split) and your window will split in two. You can move between the two halves of the window with Ctrl + a and TAB .

By default the new window will be empty, but you can switch another window into that place by using Ctrl + a repeatedly, as we've already covered.

To make it more obvious which window you're currently working in you can setup a small status bar across the bottom of the window which show you how many windows you have open, and which one you are working with.

To do this create a file .screenrc in your home area and place the following inside it:

# An alternative hardstatus to display a bar at the bottom listing the # windownames and highlighting the current windowname in blue. (This is only # enabled if there is no hardstatus setting for your terminal) hardstatus on hardstatus alwayslastline hardstatus string "%{.bW}%-w%{.rW}%n %t%{-}%+w %=%{..G} %H %{..Y} %m/%d %C%a "

(You can override all the default settings in your local file, or update the global file in /etc/screenrc to affect all users).

To sum up we've seen that screen is a very powerful tool for executing commands that run for a long time, and running multiple programs all at the same time in a single session.

We've not covered all the options, just enough to get started with. Running man screen will give you lots more options.

Here's a list of all the commands we've covered: