NASA Insignia
Site Title

/usr/bin/screen - an introduction

I originally wrote this introduction to screen in September 1994, at a time when screen was an open source add-on to operating systems such as SunOS. It is now a standard, included utility for macOS, Linux, etc. It was invaluable for getting past the frustration of getting cut off from dial-up sessions, without having a long-running process getting interrupted. Same can be true for VPN today!

There is a new utility on the EGRET cluster called 'screen' (usually /usr/bin/screen). It functions rather like a one-window window-manager, but with some additional highly valuable features. I think this could be of tremendous utility to users who wish to start a job at work and be able to check on it from home.

To start it, just type "screen" (without the quotation marks, of course) in a terminal window . When screen starts up, you will see a copyright display, at which you can hit return or spacebar to be placed in a new shell.

All commands within screen are accessed by first typing control-A (henceforth, ^A) and then the command. For example, help is "^A ?".

If you now type ^A^C or ^A c, you will create a new session within screen. You can do this at any point, even if you are not at a shell prompt. That is, you can be in vi, elm, or any other subprocess and still create a new window.

To move between sessions: (precede with ^A again for each)

  • Next window: ^@ ^N sp n
  • Previous window: ^H ^P p ^? (where ^H and ^? are also backspace and delete)

You can have up to 10 sessions within one invocation of screen. You can move through them sequentially, as shown with these 'next' and 'previous' commands, or you can jump to a particular session: ^A 0 goes to the first session ^A 4 goes to the fifth session.

Already, you can see the power of this: From a single dialup telephone connection, you can run multiple shells simultaneously, without needing anything fancy such as a PPP or SLIP account.

Attaching and Detaching

Another very powerful feature is the ability of screen to "detach". If you type ^A ^D you will be "detached" from that invocation of screen. All of the sessions and jobs that you had running continue, but you cannot see them. You can then go down to the hall to someone else's workstation and telnet ssh back to the machine from which you were running screen and "re-attach" to the screen session. Similarly you can run a screen from GSFC and then reattach to it from home.

Screen also has a default such that it will "auto-detach" if it sees a hangup signal (such as killing an xterm window that contained screen or losing a modem connection). Thus, if you are using screen from home, and you were in the middle of a long email message and had not saved it when your connection was broken, you can simply dial in again and pick up right where you left off, having lost nothing.

If you get home and want to check a session you left running, but you realize that you forgot to detach it first, you can force it to be detached at the remote location and immediately connect to it.

Also, when you first log in, you can check whether you have any screen sessions active. (This is a good idea, so that you don't leave anything dangling.) The command for this is 'screen -ls' or 'screen -list'. The output looks something like this:

	There is a possible screen on:
        	27761.ttyp4.egret       (Detached)
	1 Socket in /tmp/screens/S-dpf/.
or
	There are several screens on:
        	27761.ttyp4.egret       (Detached)
        	11732.ttyq9.egret       (Attached)
	2 Sockets in /tmp/screens/S-dpf/.

If there is just one "screen" running, then you can re-attach to it with "screen -r". If there are multiple "screen"s then you must specify to which to reattach: "screen -r ttyp4.egret". [It is recommended that you do not run multiple invocations of screen on the same machine, largely due to issues of confusion. But if you happen to do so by accident, this paragraph shows how choose.]

Summary of Attach and Detach Commands

to check what exists screen -ls
or
screen -list
to reattach screen -r
to detach elsewhere and reattach screen -r -d
to detach within screen ^A ^D
How to exit and close down screen Type ^D or exit (as usual) to close a particular session.
You can also use ^A ^K or ^A k to kill the current window.
If you want to quit all of the windows at once, type ^A ^\.
If you are exiting the last session of screen (or quitting, as above), it will display "[screen is terminating]" and return you to your original shell window. (This will disable the emacs and tcsh ^K command which kills (deletes) to the end of the current line.)

Notes:

  1. You can have more than one "screen" running at a time (with each having up to ten windows). That is not discussed here.
  2. Screen runs locally on each machine. That is, you cannot reattach to a session on egret that you began on gamma, except by using telnet or rlogin ssh to log into gamma.
'who' and 'finger' return different results depending upon whether your screen process is attached:
when screen is detached or not running:
dpf      David Friedlander     r8      Mon 15:34  flash.gsfc.nasa.
when screen is active:
dpf      David Friedlander     rb      Mon 15:45  flash:S.0
dpf      David Friedlander     rd   43 Mon 15:49  flash:S.1
dpf      David Friedlander     re      Mon 15:52  flash:S.2      

Note that it shows that I have three sessions running within screen, and they are identified in a fashion similar to that of the DISPLAY environment variable. Also, the full 'flash.gsfc.nasa.gov' entry does not show up when screen is attached. Because of this behavior, "screen -ls" is a much better way to see what the status of screen is.

Screen Running in Different Sized Windows

If you run the same screen session from both home and work, your terminal at home may show a different number of lines from the xterm window at work. To correct this, type 'resize'. If this still does not do the trick, you can set it by hand, with 'stty rows correct_number_rows', such as 'stty rows 34'.

Changing Control-A to Something Else

Screen can be customized in many ways, all detailed in the man page. One is worth mentioning here: Control-A itself is used in both emacs and the tcsh (and bash, zsh, etc) shells to jump to the beginning of the line. Screen overrides or disables that behavior. If that bothers you, you can create a ~/.screenrc file (another rc file!!) with a line of the following form:

	escape ^w
(or whatever you choose). [In this case type the caret (^) and the letter, not the actual control-letter combination.]

Saving What's on the Display

Screen offers two choices:

  • Hardcopy (^A h) makes a file called "hardcopy.n" in your home directory, where 'n' is the number of the session. (It overwrites the last saved file of the same name by default, unless you change it to append it. See the man page.) This is simply a copy of what you can see in the display of the current session.
  • Log (^A H) creates a copy called screenlog.n. (Actually, this command toggles whether data is being appended to the log or not.)
There is also a scrollback buffer (default 100 lines) for each session.

Help Screen

Although you can view the following help summary with '^A ?', sometimes it is useful to have it on paper (or in a browser window!) in front of you:


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

As you start to use it more, I very much recommend reading through the manual page (28 pages when printed out!). However, I hope you have found this document a useful introduction which enables you to start using screen.


David Friedlander, September 1994 (orig), May 2020 updated & conv to html