Chapter 1. Using GNU Screen

Tim Shaggy Bielawa

Author  

Table of Contents

Screen Basics
Starting Screen
Command Keys
Scrollback in Screen
Detaching and Reattaching Screen Sessions
Howto
Attaching already Attached Screens
Navigating around in Screen
Creating a New Window
Navigating Between Windows
Closing Windows
More Information & Customization

Some tasks you may have to perform will take a long time to finish, GNU screen is a great command line tool to use to make those tasks easier. Screen has a lot of other powerful uses too, in addition to detachment. At the end of this chapter you'll know how to start screen, detach a screen session, reattach a screen, create new shells inside a screen, switch between them, and do some basic screen configuration.

Screen Basics

Starting Screen

Starting screen is easy! Example 1.1, “Starting Screen” demonstrates starting screen.

Example 1.1. Starting Screen

<tbielawa>@(shell001)[~] 01:07:34
$ screen

Easy right? Your screen should have blanked and returned with a normal prompt at the top of your terminal.

Note

Your scrollback buffer won't work like normal now. Scrollback is explained in the following sections.

Example 1.2. Exiting Screen

<tbielawa>@(shell001)[~] 01:07:34
$ exit
[screen is terminating]
<tbielawa>@(shell001)[~] 01:07:56
$ 

As you can see in Example 1.2, “Exiting Screen”, exiting screen works like exiting a normal shell. The normal C-d sequence still works for exiting a shell.

Command Keys

Throughout this chapter you'll see command sequences displayed in the following format: C-a x. This means to press the Control key and the a key at the time then press the x key afterwards. C-a is the standard sequence to press when starting a screen command.

Note

Depending on the documentation you solicit you will find the "Control+x" sequence written in a few different ways. ^x is one common way, C-x the other most common way.

Scrollback in Screen

You can still scroll back in screen. The default scrollback buffer is usually set around 1000 lines, much more than the default 300 or so in your normal bash shell. You can't Shift-PgUp like normal to see it though. To scroll in screen you need to enter "copy/scrollback" mode.

C-a Esc is the sequence to enter scrollback mode. When entering, should get a block cursor on your screen, the cursor indicates where you are for selection commands. Use the Up, Down, Page Up, and Page Down keys to navigate. When you're finished you can just press Escape once or twice to exit copy/scrollback mode.

Detaching and Reattaching Screen Sessions

The ability to detach and reattach screen sessions is arguably the most powerful functionality screen provides. A detached screen behaves similar to a backgrounded task in that it continues to run uninterrupted leaving your shell free to run other tasks. Screen sessions differ from backgrounded tasks in that they are persistent through logouts and aren't affected by terminating the parent process (usually a shell) that launched them. A detached screen can be resumed at any time from any shell. This is exactly what makes screen detaching so much more useful than just backgrounding long running tasks.

Tasks you'd want to detach

  • dputting large packages

  • (de)compressing large archives

  • restoring from backups

  • transferring large files

Howto

Detaching is super simple, use this command sequence: C-a d. If you were successful, you should see something like Example 1.3, “Detaching a Screen”.

Example 1.3. Detaching a Screen

<tbielawa>@(expressomaker)[~] 01:46:01
$ screen
[detached]
<tbielawa>@(expressomaker)[~] 02:31:42
$ 

Reattaching a detached screen session is generally a trivial task. If your user has multiple screens detached simultaniously you will have to tell screen which one you want to reattach, Example 1.4, “Reattaching a Screen When Only One is Detached” demonstrates.

Example 1.4. Reattaching a Screen When Only One is Detached

<tbielawa>@(expressomaker)[~] 01:46:01
$ screen -R

The screen man page describes -R thusly: "attempts to resume the first detached screen session it finds. If successful, all other command-line options are ignored. If no detached session exists, starts a new session using the specified options, just as if -R had not been specified."

You may find yourself in the situation where you've been a busy bee and have detached numerous screen sessions. Reattaching the correct one can turn into a game of guesswork since detached screen sessions are identified with names like 12865.pts-0.shell003, or 14500.pts-4.fridge. As you can see, names are a combination of the PID of the screen session, the TTY the screen is running on, and the hostname of the machine the screen is running on. To help sort through this mess you can draw from the fact that PIDs are assigned sequentially (though they do cycle back around), shells opened in X sessions (gnome-terminal, terminator, etc...) have enames like 'ttys005' while shells opened in SSH sessions typically are named like 'pts-3'. Isn't it lucky that you've just read the section on how to detach a screen!

Here's a practical example wherein I had SSHd into an LDAP slave from an office machine, began resyncing it, disconnected the SSH session and went home, Example 1.5, “Reattaching When Multiple Screens are Detached (Part-A)”. When I got home I SSHd into the LDAP slave again to check up on it, upon doing so finding out that I must select which screen session I wanted to reattach to, Example 1.6, “Reattaching When Multiple Screens are Detached (Part-B)”.

Example 1.5. Reattaching When Multiple Screens are Detached (Part-A)

<tbielawa>@(sinister)[~] 03:07:34
$ ssh root@ldapslave002
Last login: Sun Aug  2 03:12:22 2009 from 157.182.95.88
Linux ldapslave002 2.6.15-54-server #1 SMP Thu Apr 2 21:27:55 UTC 2009 i686 GNU/Linux

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
root@ldapslave002:~# screen
root@ldapslave002:~# invoke-rc.d slapd stop
Stopping OpenLDAP: slapd.
root@ldapslave002:~# rm -fR /srv/ldap/*
root@ldapslave002:~# slapadd < 2009-08-02
[detached]
root@ldapslave002:~# logout
Connection to ldapslave002 closed.
<tbielawa>@(sinister)[~] 03:10:59
$ exit

Note

At this point my screen session is running detached on ldapslave002.

Example 1.6. Reattaching When Multiple Screens are Detached (Part-B)

<tbielawa>@(expressomaker)[~] 03:25:55
$ ssh root@ldapslave002.lcsee.wvu.edu
Warning: Permanently added 'ldapslave002.lcsee.wvu.edu' (RSA) to the list of known hosts.
Last login: Sun Aug  2 03:14:15 2009 from 157.182.194.176
Linux ldapslave002 2.6.15-54-server #1 SMP Thu Apr 2 21:27:55 UTC 2009 i686 GNU/Linux

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
root@ldapslave002:~# screen -R
There are several suitable screens on:
        4503.pts-4.ldapslave002(06/17/2009 12:33:35 AM)(Detached)
        14151.pts-2.ldapslave002(08/02/2009 03:10:55 AM)(Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.
root@ldapslave002:~# screen -R 14151.pts-2.ldapslave002
root@ldapslave002:~# invoke-rc.d slapd stop
Stopping OpenLDAP: slapd.
root@ldapslave002:~# rm -fR /srv/ldap/*
root@ldapslave002:~# slapadd < 2009-08-02
root@ldapslave002:~# invoke-rc.d slapd start
Starting OpenLDAP: slapd.
root@ldapslave002:~# exit
[screen is terminating]
root@ldapslave002:~# exit
Connection to ldapslave002.lcsee.wvu.edu closed.
<tbielawa>@(expressomaker)[~] 03:17:30
$ exit

Note

While it may look as though after I reattached the screen that I retyped all my commands from before, that is incorrect. Screen picked up from exactly where I left off. Furthermore, keep in mind that every time you create a new screen, detach your present screen, or resume a detached screen, that your shell will blank out and then be redrawn. The result is always the "last thing" you were doing, be that launching screen, or your work inside an screen.

Attaching already Attached Screens

This section explains the advanced topic topic of attaching a screen session that is presently open on a seperate shell to your present shell. Useful in cases where you forgot to detach a screen previously and no longer have access to the console the screen is running on, or where something went horribly wrong and screen doesn't think a screen is actually detached to begin with. Or, perhaps instead you have a screen open in gnome and have dropped to a virtual console where you would like to have access to it.

If there is only one screen session running you can trivially reattach it to your present shell by launching screen with the -D -R options. The screen man page describes this combination thusly: "Attach here and now. In detail this means: If a session is running, then reattach. If necessary detach and logout remotely first. If it was not running create it and notify the user. This is the authors favorite." Like before, if multiple screen sessions are detached already you only must specify the session identifier after the -D -R options.

Navigating around in Screen

Screen also has the ability to host multiple shells out of one screen session without making you launch new terminal applications or switch to other TTYs. Each of these shells we call windows when in the context of screen. Before we can cover nagivating between windows we'll first have to cover how to create new windows.

Creating a New Window

Creating a new window is simple, just use the C-a c key binding. Your display will now show a new shell with your default prompt. An interactive listing of all the windows associated with this screen is available through the C-a " key binding.

Navigating Between Windows

Navigation is simple, use these key bindings to get around.

Navigation Bindings

  • Next window: C-a n

  • Previous Window (1): C-a p

  • Previous Window (2) C-a backspace

  • Toggle to the window displayed previously: C-a C-a

  • Jump to window #x: C-a x

Closing Windows

Closing windows in screen deserves special note -- not because it's difficult -- but because screen is different from your normal shell with respect to closing windows.

Screen will not be affected by a SIGHUP to your remote connection (dropping your SSH connection), the process that launched it terminating (closing your terminal), or even you logging out. In those respects, screen's resilience is matched only by roaches, styrofoam, and twinkies. Actually ending a screen session requires closing all of the windows associated with that screen. If you find yourself in the situation where screen is handling many windows you may find it convenient to use the quit macro. The quit macro is: C-a C-\. You will be asked to confirm your action with the prompt: Really quit and kill all your windows [y/n]. You will know you have closed screen when you see the message [screen is terminating] followed by a fresh prompt.

More Information & Customization

In this chapter I've only just begun to touch on all of the functionality provided by screen. With screen you can also: copy and paste text between windows, configure default window groups, display multiple windows at the same time, write your paste buffer to a file, named windows, and the list goes on...

Screen has a man page.

The man page lists and describes all of the other key bindings that I haven't mentioned here. It also describes the variables allowed in a screenrc file.

Get rid of the welcome window

I can't stand the welcome window in screen. That's why I always set startup_message to off in /etc/screenrc on my machines.

Macking Backspace Work with OS X + Screen + Emacs

You may find yourself geting Wuff'd at, or getting a lot of help, if you're using OS X to run emacs inside of a screen session on a remote Linux host and use the backspace key. The backspace key gets mangled when it goes through all of that. The "solution" (i.e., only thing I found to work) requires minor reconfiguration of the OS X Terminal application. In Terminal, go to TerminalPreferences, and then select Advanced. Now where it says Declare terminal as: select rxvt. Also make sure that Delete sends Ctrl-H isn't selected. Finally, under the Keyboard tab, check that Use option as meta key is selected. Be aware: altering your terminal type may require adjusting your .bash* file to to account for rxvt as a possible value of the $TERM variable when setting your $PS1 prompt string.