Home Blog CV Projects Patterns Notes Book Colophon Search

GNU Screen

19 May, 2007

If you've had a situation where you are running a command on a remote server and need to close your terminal to go home or use another computer but don't want to stop the command then GNU screen is for you. The website says:

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

In simple terms though it just a piece of software that separates what you are doing in a particular terminal from the terminal itself so that you close your terminal without affecting what is actually going on. It also has another use, it enables you to run multiple terminal sessions from the same actual terminal and switch between them at will.

To install it type:

sudo apt-get install screen

Here are some examples of its use.

Load up two terminals, we'll call them A and B. In terminal A run the screen command. You'll see the following message:

Screen version 4.00.03 (FAU) 23-Oct-06

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 enter and you will see what looks like an ordinary prompt. Try running a command:

bose:/home/james# echo "This is screen window 0"
This is screen window 0

At this point we want to send a command to screen. screen commands are issued by pressing Ctrl+a and then the letter for the particular command. To detach from the screen session and go back to the terminal prompt press Ctrl+a then d. A message appears to tell you that you have detached from screen:

bose:/home/james# screen
[detached]

You can then reattach to screen using screen -r and you will see that the screen window is exactly as you left it:

bose:/home/james# echo "This is screen window 0"
This is screen window 0

Now we are reattached to screen we will create a new screen window. A screen window is effectively just another virtual terminal running within the screen program. You can create a new screen window by pressing Ctrl+a and then c for create. At this prompt you can enter this:

bose:/home/james# echo "This is screen window 1"
This is screen window 1

Now that we have two screen windows running we can toggle between them by pressing Ctrl+a and then Ctrl+a again. You can also get a list of all the screen windows by pressing Ctrl+a then ". You'll see this:

Num Name                                                                   Flags
  0 bash                                                                       $
  1 bash                                                                       $

Notice that the default name for each window is the name of the shell you are running, in this case bash. You can rename the current screen window you are using by pressing Ctrl+a then A.

Now that we have a fairly typical screen session we can have a go at attaching it to the other terminal. In the terminal you have been using type Ctrl+a and d and in terminal B type screen -r you will see the screen session you just detached from terminal A. Nifty eh?

To close a screen window you can type exit or to kill the screen window type Ctrl+a then k.

Other things you might like to try include Ctrl+a then S to split a screen in two and Ctrl+a and Tab to switch between panes.

There is some more information here: http://www.kuro5hin.org/story/2004/3/9/16838/14935

Copyright James Gardner 1996-2020 All Rights Reserved. Admin.