Unix Screen Utility - How do I use that?

October 20, 2007

DESCRIPTION

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 a DEC VT100 terminal and, in addition, several control functions from the ISO 6429 (ECMA 48, ANSI X3.64) and ISO 2022 standards (e.g. insert/delete line and support for multiple character sets). There is a scrollback history buffer for each virtual terminal and a copy-and-paste mechanism that allows moving text regions between windows.

When screen is called, it creates a single window with a shell in it (or the specified command) and then gets out of your way so that you can use the program as you normally would. Then, at any time, you can create new (full-screen) windows with other programs in them (including more shells), kill existing windows, view a list of windows, turn output logging on and off, copy-and-paste text between windows, view the scrollback history, switch between windows in whatever manner you wish, etc. All windows run their programs completely independent of each other. Programs continue to run when their window is currently not visible and even when the whole screen session is detached from the user’s terminal. When a program terminates, screen (per default) kills the window that contained it. If this window was in the foreground, the display switches to the previous window; if none are left, screen exits.

Everything you type is sent to the program running in the current window. The only exception to this is the one keystroke that is used to initiate a command to the window manager. By default, each command begins with a control-a (abbreviated C-a from now on), and is followed by one other keystroke. The command character and all the key bindings can be fully customized to be anything you like, though they are always two characters in length.

Screen does not understand the prefix “C-” to mean control. Please use the caret notation (”^A” instead of “C-a”) as arguments to e.g. the escape command or the -e option. Screen will also print out control characters in caret notation.

The standard way to create a new window is to type “C-a c”. This creates a new window running a shell and switches to that window immediately, regardless of the state of the process running in the current window. Similarly, you can create a new window with a custom command in it by first binding the command to a keystroke (in your .screenrc file or at the “C-a :” command line) and then using it just like the “C-a c” command. In addition, new windows can be created by running a command like:

screen emacs prog.c

from a shell prompt within a previously created window. This will not run another copy of screen, but will instead supply the command name and its arguments to the window manager (specified in the $STY environment variable) who will use it to create the new window. The above example would start the emacs editor (editing prog.c) and switch to its window.

If “/etc/utmp” is writable by screen, an appropriate record will be written to this file for each window, and removed when the window is terminated. This is useful for working with “talk”, “script”, “shutdown”, “rsend”, “sccs” and other similar programs that use the utmp file to determine who you are. As long as screen is active on your terminal, the terminal’s own record is removed from the utmp file. See also “C-a L”.

GETTING STARTED

Before you begin to use screen you’ll need to make sure you have correctly selected your terminal type, just as you would for any other termcap/terminfo program. (You can do this by using tset for example.) If you’re impatient and want to get started without doing a lot more reading, you should remember this one command: “C-a ?”. Typing these two characters will display a list of the available screen commands and their bindings. Each keystroke is discussed in the section “DEFAULT KEY BINDINGS”. The manual section “CUSTOMIZATION” deals with the contents of your .screenrc.

If your terminal is a “true” auto-margin terminal (it doesn’t allow the last position on the screen to be updated without scrolling the screen) consider to use a version of your terminal’s termcap that has automatic margins turned off. This will ensure an accurate and optimal update of the screen in all circumstances. Most terminals nowadays have “magic” margins (automatic margins plus usable last column). This is the VT100 style type and perfectly suited for screen. If all you’ve got is a “true” auto-margin terminal screen will be content to use it, but updating a character put into the last position on the screen may not be possible until the screen scrolls or the character is moved into a safe position in some other way. This delay can be shortened by using a terminal with insert-character capability.

COMMAND-LINE OPTIONS

Screen has the following command-line options:

-a
include all capabilities (with some minor exceptions) in each window’s termcap, even if screen must redraw parts of the display in order to implement a function.
-A
Adapt the sizes of all windows to the size of the current terminal. By default, screen tries to restore its old window sizes when attaching to resizable terminals (those with “WS” in its description, e.g. suncmd or some xterm).
-c file
override the default configuration file from “$HOME/.screenrc” to file.
-d|-D [pid.tty.host]
does not start screen, but detaches the elsewhere running screen session. It has the same effect as typing “C-a d” from screen’s controlling terminal. -D is the equivalent to the power detach key. If no session can be detached, this option is ignored. In combination with the -r/-R option more powerful effects can be achieved:
-d -r
Reattach a session and if necessary detach it first.
-d -R
Reattach a session and if necessary detach or even create it first.
-d -RR
Reattach a session and if necessary detach or create it. Use the first session if more than one session is available.
-D -r
Reattach a session. If necessary detach and logout remotely first.
-D -R
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 author’s favorite.
-D -RR
Attach here and now. Whatever that means, just do it.
Note: It is always a good idea to check the status of your sessions by means of “screen -list”.
-e xy
specifies the command character to be x and the character generating a literal command character to y (when typed after the command character). The default is “C-a” and `a’, which can be specified as “-e^Aa”. When creating a screen session, this option sets the default command character. In a multiuser session all users added will start off with this command character. But when attaching to an already running session, this option changes only the command character of the attaching user. This option is equivalent to either the commands “defescape” or “escape” respectively.
-f, -fn, and -fa
turns flow-control on, off, or “automatic switching mode”. This can also be defined through the “defflow” .screenrc command.
-h num
Specifies the history scrollback buffer to be num lines high.
-i
will cause the interrupt key (usually C-c) to interrupt the display immediately when flow-control is on. See the “defflow” .screenrc command for details. The use of this option is discouraged.
-l and -ln
turns login mode on or off (for /etc/utmp updating). This can also be defined through the “deflogin” .screenrc command.
-ls and -list
does not start screen, but prints a list of pid.tty.host strings identifying your screen sessions. Sessions marked `detached’ can be resumed with “screen -r”. Those marked `attached’ are running and have a controlling terminal. If the session runs in multiuser mode, it is marked `multi’. Sessions marked as `unreachable’ either live on a different host or are `dead’. An unreachable session is considered dead, when its name matches either the name of the local host, or the specified parameter, if any. See the -r flag for a description how to construct matches. Sessions marked as `dead’ should be thoroughly checked and removed. Ask your system administrator if you are not sure. Remove sessions with the -wipe option.
-L
tells screen your auto-margin terminal has a writable last-position on the screen. This can also be set in your .screenrc by specifying `LP’ in a “termcap” command.
-m
causes screen to ignore the $STY environment variable. With “screen -m” creation of a new session is enforced, regardless whether screen is called from within another screen session or not. This flag has a special meaning in connection with the `-d’ option:
-d -m
Start screen in “detached” mode. This creates a new session but doesn’t attach to it. This is useful for system startup scripts.
-D -m
This also starts screen in “detached” mode, but doesn’t fork a new process. The command exits if the session terminates.
-O
selects a more optimal output mode for your terminal rather than true VT100 emulation (only affects auto-margin terminals without `LP’). This can also be set in your .screenrc by specifying `OP’ in a “termcap” command.
-q
Suppress printing of error messages. In combination with “-ls” the exit value is as follows: 9 indicates a directory without sessions. 10 indicates a directory with running but not attachable sessions. 11 (or more) indicates 1 (or more) usable sessions. In combination with “-r” the exit value is as follows: 10 indicates that there is no session to resume. 12 (or more) indicates that there are 2 (or more) sessions to resume and you should specify which one to choose. In all other cases “-q” has no effect.
-r [pid.tty.host]
-r sessionowner/[pid.tty.host]
resumes a detached screen session. No other options (except combinations with -d/-D) may be specified, though an optional prefix of [pid.]tty.host may be needed to distinguish between multiple detached screen sessions. The second form is used to connect to another user’s screen session which runs in multiuser mode. This indicates that screen should look for sessions in another user’s directory. This requires setuid-root.
-R
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. The option is set by default if screen is run as a login-shell (actually screen uses “-xRR” in that case). For combinations with the -d/-D option see there.
-s
sets the default shell to the program specified, instead of the value in the environment variable $SHELL (or “/bin/sh” if not defined). This can also be defined through the “shell” .screenrc command.
-S sessionname
When creating a new session, this option can be used to specify a meaningful name for the session. This name identifies the session for “screen -list” and “screen -r” actions. It substitutes the default [tty.host] suffix.
-t name
sets the title (a.k.a.) for the default shell or specified program. See also the “shelltitle” .screenrc command.
-U
Run screen in UTF-8 mode. This option tells screen that your terminal sends and understands UTF-8 encoded characters. It also sets the default encoding for new windows to `utf8′.
-v
Print version number.
-wipe [match]
does the same as “screen -ls”, but removes destroyed sessions instead of marking them as `dead’. An unreachable session is considered dead, when its name matches either the name of the local host, or the explicitly given parameter, if any. See the -r flag for a description how to construct matches.
-x
Attach to a not detached screen session. (Multi display mode).
-X
Send the specified command to a running screen session. You can use the -d or -r option to tell screen to look only for attached or detached screen sessions. Note that this command doesn’t work if the session is password protected.

DEFAULT KEY BINDINGS

As mentioned, each screen command consists of a “C-a” followed by one other character. For your convenience, all commands that are bound to lower-case letters are also bound to their control character counterparts (with the exception of “C-a a”; see below), thus, “C-a c” as well as “C-a C-c” can be used to create a window. See section “CUSTOMIZATION” for a description of the command.

The following table shows the default key bindings:
C-a ‘ (select)
Prompt for a window name or number to switch to.
C-a (windowlist -b)
Present a list of all windows for selection.
C-a 0 (select 0)
C-a 9 (select 9)
C-a - (select -)
Switch to window number 0 - 9, or to the blank window.
C-a tab (focus)
Switch the input focus to the next region.
C-a C-a (other)
Toggle to the window displayed previously.
Note that this binding defaults to the command character typed twice, unless overridden. For instance, if you use the option “-e]x“, this command becomes “]]”.
C-a a (meta)
Send the command character (C-a) to window. See escape command.
C-a A (title)
Allow the user to enter a name for the current window.
C-a b
C-a C-b (break)
Send a break to window.
C-a B (pow_break)
Reopen the terminal line and send a break.
C-a c
C-a C-c (screen)
Create a new window with a shell and switch to that window.
C-a C (clear)
Clear the screen.
C-a d
C-a C-d (detach)
Detach
screen from this terminal.
C-a D D (pow_detach)
Detach and logout.
C-a f
C-a C-f (flow)
Toggle flow on, off or auto.
C-a F (fit)
Resize the window to the current region size.
C-a C-g (vbell)
Toggles
screen’s visual bell mode.
C-a h (hardcopy)
Write a hardcopy of the current window to the file “hardcopy.n“.
C-a H (log)
Begins/ends logging of the current window to the file “screenlog.n“.
C-a i
C-a C-i (info)
Show info about this window.
C-a k
C-a C-k (kill)
Destroy current window.
C-a l
C-a C-l (redisplay)
Fully refresh current window.
C-a L (login)
Toggle this windows login slot. Available only if
screen is configured to update the utmp database.
C-a m
C-a C-m (lastmsg)
Repeat the last message displayed in the message line.
C-a M (monitor)
Toggles monitoring of the current window.
C-a space
C-a n
C-a C-n (next)
Switch to the next window.
C-a N (number)
Show the number (and title) of the current window.
C-a backspace
C-a h
C-a p
C-a C-p (prev)
Switch to the previous window (opposite of C-a n).
C-a q
C-a C-q (xon)
Send a control-q to the current window.
C-a Q (only)
Delete all regions but the current one.
C-a r
C-a C-r (wrap)
Toggle the current window’s line-wrap setting (turn the current window’s
automatic margins on and off).
C-a s
C-a C-s (xoff)
Send a control-s to the current window.
C-a S (split)
Split the current region into two new ones.
C-a t
C-a C-t (time)
Show system information.
C-a v (version)
Display the version and compilation date.
C-a C-v (digraph)
Enter digraph.
C-a w
C-a C-w (windows)
Show a list of window.
C-a W (width)
Toggle 80/132 columns.
C-a x
C-a C-x (lockscreen)
Lock this terminal.
C-a X (remove)
Kill the current region.
C-a z
C-a C-z (suspend)
Suspend
screen. Your system must support BSD-style job-control.
C-a Z (reset)
Reset the virtual terminal to its “power-on” values.
C-a . (dumptermcap)
Write out a “.termcap” file.
C-a ? (help)
Show key bindings.
C-a C-\ (quit)
Kill all windows and terminate
screen.
C-a : (colon)
Enter command line mode.
C-a [
C-a C-[
C-a esc (copy)
Enter copy/scrollback mode.
C-a ] (paste .)
Write the contents of the paste buffer to the stdin queue of the
current window.
C-a {
C-a } (history)
Copy and paste a previous (command) line.
C-a > (writebuf)
Write paste buffer to a file.
C-a < (readbuf)
Reads the screen-exchange file into the paste buffer.
C-a = (removebuf)
Removes the file used by C-a < and C-a >.
C-a , (license)
Shows where
screen comes from, where it went to and why you can use it.
C-a _ (silence)
Start/stop monitoring the current window for inactivity.
C-a * (displays)
Show a listing of all currently attached displays.
 

Read the full article

 

2 Comments for “Unix Screen Utility - How do I use that?”

  1. Lorraine Says:

    I use screen all the time. A real time saver!

  2. Gaurav Says:

    Hi,

    I want to group 10 unix commands in one shell script which while invoked can run these 10 jobs in parallel, as one job serves the input to the other.

    So can I use “screen” command to open a new window each time running a job? and if yes then how should i handle the authentication as it need username and password at every login.

    thanks in advance.

Post a comment

Name (required)

Mail (will not be published) (required)

Website

*
To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
Click to hear an audio file of the anti-spam word