Screen

How to use Screen.

 

Screen is a console application that allows you to use multiple terminal sessions within one window. The program operates within a shell session and acts as a container and manager for other terminal sessions, similar to how a window manager manages windows.

There are many situations where creating several terminal windows is not possible or ideal. You might need to manage multiple console sessions without an X server running, you might need to access many remote cloud servers easily, or you might need to monitor a running program’s output while working on some other task. All of needs are easily addressed with the power of screen.

 

Basic Usage


To start a new screen session, we simply type the “screen” command.

screen
Screen version 4.00.03jw4 (FAU) 2-May-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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

Send bugreports, fixes, enhancements, t-shirts, money, beer & pizza to
This email address is being protected from spambots. You need JavaScript enabled to view it.
 


                  [Press Space for next page; Return to end.]

You’ll be greeted with the licensing page upon starting the program. Just press “Return” or “Enter” to continue.

What happens next may be surprising. We are given a normal command prompt and it looks like nothing has happened. Did “screen” fail to run correctly? Let’s try a quick keyboard shortcut to find out. Hold the control key, and then hit the “a” key, followed by the “v” key:

Ctrl-a v
screen 4.00.03jw4 (FAU) 2-May-06

We’ve just requested the version information from screen, and we’ve received some feedback that allows us to verify that screen is running correctly.

Now is a great time to introduce the way that we will be controlling screen. Screen is mainly controlled through keyboard shortcuts. Every keyboard shortcut for screen is prefaced with “Ctrl-a” (hold the control key while pressing the “a” key). That sequence of keystrokes tells screen that it needs to pay attention to the next keys we press.

You’ve already used this paradigm once when we requested the version information about screen. Let’s use it to get some more useful information.

Ctrl-a ?
                       Screen key bindings, page 1 of 2.

                       Command key:  ^A   Literal ^A:  a

  break       ^B b         license     ,            removebuf   =         
  clear       C            lockscreen  ^X x         reset       Z         
  colon       :            log         H            screen      ^C c      
  copy        ^[ [         login       L            select      '         
  detach      ^D d         meta        a            silence     _         
  digraph     ^V           monitor     M            split       S         
  displays    *            next        ^@ ^N sp n   suspend     ^Z z      
  dumptermcap .            number      N            time        ^T t      
  fit         F            only        Q            title       A         
  flow        ^F f         other       ^A           vbell       ^G        
  focus       ^I           pow_break   B            version     v         
  hardcopy    h            pow_detach  D            width       W         
  help        ?            prev        ^H ^P p ^?   windows     ^W w      
  history     { }          quit        \            wrap        ^R r      
  info        i            readbuf              
  kill        K k          redisplay   ^L l         xoff        ^S s      
  lastmsg     ^M m         remove      X            xon         ^Q q      

                  [Press Space for next page; Return to end.]

This is the internal keyboard shortcut screen. You’ll probably want to memorize how to get here, because it’s an excellent quick reference. As you can see at the bottom, you can press “space” to get more commands.

Okay, let’s try something more fun. Let’s run a program called “top” in this window, which will show us some information on our processes.

top
top - 16:08:07 up  1:44,  1 user,  load average: 0.00, 0.01, 0.05
Tasks:  58 total,   1 running,  57 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:    507620k total,   262920k used,   244700k free,     8720k buffers
Swap:        0k total,        0k used,        0k free,   224584k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND           
    1 root      20   0  3384 1836 1288 S  0.0  0.4   0:00.70 init               
    2 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthreadd           
    3 root      20   0     0    0    0 S  0.0  0.0   0:00.11 ksoftirqd/0        
    5 root      20   0     0    0    0 S  0.0  0.0   0:00.12 kworker/u:0        
    6 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/0        
    7 root      RT   0     0    0    0 S  0.0  0.0   0:00.07 watchdog/0         
    8 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 cpuset             
    9 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 khelper            
   10 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kdevtmpfs          
   11 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 netns              
   12 root      20   0     0    0    0 S  0.0  0.0   0:00.03 sync_supers        
   13 root      20   0     0    0    0 S  0.0  0.0   0:00.00 bdi-default        
   14 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 kintegrityd        
   15 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 kblockd            
   16 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 ata_sff            
   17 root      20   0     0    0    0 S  0.0  0.0   0:00.00 khubd              
   18 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 md

Okay, we are now monitoring the processes on our VPS. But what if we need to run some commands to find out more information about the programs we see? We don’t need to exit out of “top”. We can create a new window to run these commands.

Ctrl-a c

The “Ctrl-a c” sequence creates a new window for us. We can now run whatever commands we want without disrupting the monitoring we were doing in the other window.

Where did that other window go? We can get back to it using a new command:

Ctrl-a n

This sequence goes to the next window that we are running. The list of windows wrap, so when there aren’t windows beyond the current one, it switches us back to the first window.

Ctrl-a p

This sequence changes the current window in the opposite direction. So if you have three windows and are currently on the third, this command will switch you to the second window.

A helpful shortcut to use when you’re flipping between the same two windows is this:

Ctrl-a Ctrl-a

This sequence moves you to your most recently visited window. So in the previous example, this would move you back to your third window.

At this point, you might be wondering how you can keep track of all of the windows that we are creating. Thankfully, screen comes with a number of different ways of managing your different sessions. First, we’ll create three new windows for a total of four windows and then we’ll try out one of the simplest window management tools, “Ctrl-a w”.

Ctrl-a c
Ctrl-a c
Ctrl-a c
Ctrl-a w
0$ bash  1$ bash  2-$ bash  3*$ bash

We get some useful information from this command: a list of our current windows. Here, we have four windows. Each window has a number and the windows are numbered starting at “0”. The current window has an asterisk next to the number.

So you can see that we’re currently at window #3 (actually the fourth window because the first window is 0). So how do we get to window #1 quickly?

Ctrl-a 1

We can use the index number to jump straight to the window we want. Let’s see our window list again.

Ctrl-a w
0$ bash  1*$ bash  2$ bash  3-$ bash

As you can see, the asterisk tells us that we’re now on window #1. Let’s try a different way of switching windows.

Ctrl-a “
Num Name                                                                   Flags

  0 bash                                                                       $
  1 bash                                                                       $
  2 bash                                                                       $
  3 bash                                                                       $

We get an actual navigation menu this time. You can navigate with either the up and down arrows or with “j” and “k” like you would in the vi text editor. Switch to a window by pressing “Return” or “Enter”.

This is pretty useful, but right now all of our windows are named “bash”. That’s not very helpful. Let’s name some of our sessions. Switch to a window you want to name and then use the “Ctrl-a A” sequence.

Ctrl-a 0
Ctrl-a A
Set window's title to: bash

Using the “Ctrl-a A” sequence, we can name our sessions. You can now backspace over “bash” and then rename it whatever you’d like. We’re going to run “top” on window #0 again, so we’re going to name it “monitoring”.

Set window's title to: monitoring
Ctrl-a “
Num Name                                                                   Flags

  0 monitoring                                                                 $
  1 bash                                                                       $
  2 bash                                                                       $
  3 bash                                                                       $

Now we have a more helpful label for window #0. So we know how to create and name windows, but how do we get rid of them when we don’t need them anymore? We use the “Ctrl-a k” sequence, which stands for “kill”.

Ctrl-a k
Really kill this window [y/n]

Managing Screen Sessions


When you want to quit screen and kill all of your windows, you can use this command sequence.

Ctrl-a \
Really quit and kill all your windows [y/n]

This will destroy our screen session. We will lose any windows we have created and any unfinished work.

But we want to explore one of the huge benefits of using “screen”. We don’t want to destroy the session, we simply want to detach it. Detaching allows our programs in the screen instance to continue to run, but it gives us access back to our base-console session (the one where we started “screen” from initially). The screen session is still there, it will just be managed in the background.

Ctrl-a d
[detached from 1835.pts-0.Blank]

So our session is now detached. How do we get back into it?

screen –r

The “r” stands for reattach. We are now back in our screen session. What if we have multiple screen sessions though? What if we had started a screen session and detached it, and then started a new screen session and detached that as well?

screen
Ctrl-a d
screen
Ctrl-a d

How do we tell screen which session to attach?

 screen –ls
 There are screens on:
	2171.pts-0.Blank	(07/01/2013 05:00:39 PM)	(Detached)
	1835.pts-0.Blank	(07/01/2013 03:50:43 PM)	(Detached)
2 Sockets in /var/run/screen/S-justin.

Now we have a list of our sessions. We can reattach the second one by typing its id number after the “-r” flag.

screen –r 1835

What if you want to attach a session on two separate computers or terminal windows? You can use the “-x” flag, which lets you share the session.

screen –x

 

Live Free Or Die!