Wednesday, December 16, 2009

Screen: A "must" tool for Unix Administrators

SkyHi @ Wednesday, December 16, 2009
GNU Screen is a simple but at the same time flexible and programmable windows manager. It is often called terminal multiplexer, but the key functionality of screen is in the area of windows management: creation, destruction and resizing of windows. For example it can split the console screen horizontally and display two windows at the same time. Screen is a Unix program (Cygwin version exists and is quite usable). For good Windows terminal emulator see Teraterm

In addition to the ability to run many shell processes in a single terminal, screen provides classic windows management capabilities -- the ability to split terminal into regions (horizontal stripes of varying length, each containing its own session. What is also important is that like VNC screen provides the ability to detach the terminal emulator from the running programs and then reattach the same or new terminal to them.

This feature is enabled by default.

An important feature of screen is that like VNC it provides the ability to detach the terminal emulator from the running programs and then reattach the same or new terminal to them.

This means screen will keep shells and programs running in all windows that you opened even if you accidentally disconnected from the remote server or closed the terminal emulator. Like VNC screen also permits to share a terminal (in read-only mode) with someone else in real time. This permits to collaborate on a project by several remote users located in different places. It also frees administrator from waiting until a particular task ends. He/she can be connected to any number of servers, start a task, detatch from the task, and the task keeps running. Then reattach later, check the status and intervene of needed. That means that you can start a job at the office and check for it completion at home.

Screen has a certain architectural affinity to the orthodox file managers (OFM ), especially deco as well as orthodox editors like vi and THE (the power of command line).

Like in vi in screen you have to hit certain prefix to get to command line. The default prefix is Ctrl-A. For example Ctrl-A,C will open window.

This availability of command line operations and related scriptability via screenrc file makes screen attractive to system administrators who value blend of power and simplicity provided by "orthodox" tools. Like in all orthodox utilities there is an extensive set of commands (mini-language) that can used to modify the behavior of screen.

Ctrl-a is called attention/command key and is similar to Esc in vi (you also can change Ctrl-A to another sequence, if you wish). This sequence presuppose that you are using Sun keyboard (with Ctrl instead of Caps lop) or switched Ctrl and CapsLock keys (in Windows you can use Install RandyRants SharpKeys -- registry hack that permits remapping CapsLock to Alt. ).

If the colon (":") is entered after it, then screen switches to command mode and you'll see a highlighted colon prompt at the bottom of the screen. After that, like in vi, you can enter commands that change the behavior of the screen and modify its current configuration. For example you can open and close windows, change size of windows, etc. For example you open new window also by typing command

Ctrl-A: screen

which is equivalent to the shortcut Ctrl-A,C that we discussed above.

So shortcut Ctrl-A,C and command Ctrl-A: screen are synonyms. This is just an association that is defined in .screenrc file - screen's dotfile. File .screenrc is a list of commands which are executed at startup. While by default screen is using the name .screenrc you can start screen using arbitrary screen's dotfile using option -c, for example

screen -c ~myhome/.screenrc_`uname`

This example demonstrated that you can have multiple .screenrc files and use the one that suits current environment (for example Linux, AIX, Solaris, etc) and the task in hand.

You can also execute predefined sequences of screen commands stored in the files (screen scripts) using the command source. One common application is the execution of the set of command creating a particular combination of windows and loading particular applications into each window. For example, you can load Perl in debugging mode on one window, vi in another and Perl POD browser in the third creating poor man IDE environment. For example

source .screenrc_perl

or

source ~myhome/perl.screenrc

That provides you with the flexibility to have several predefined sets of windows working on a particular task, the capability that improves your productivity dramatically as you can polish the arrangement and gradually connect a set of useful shortcuts as you gain experience with the environment (in the example above, working with Perl). System administrators can (and should) have a custom part of configuration encoded for particular hosts (that's the way I use screen at work) :

source ${HOME}/${HOST}.screenrc

Due to availability of command line interface and scriptability Screen can be considered to be a member of "orthodox" family of utilities which include orthodox file managers (mc, FAR, etc) and orthodox editors (vim, Xedit, THE, etc). Command line can be activated with Ctrl-A: after that you can enter any command from the screen command set to modify behavior of the screen or execute sets of commands (screen script) from particular file using command source.

A typical and probably the simplest application of this capability is to launch a predefined set of windows at screen startup., for example:

source $HOME/.screenrc # read in your normal screenrc
screen top # now start opening windows
screen -t irc epic # with the -t option it's possible to set the window title
screen -t mail 8 mutt # you can also specify the window number to launch in
screen -t messages 9 tail -f /var/log/messages

In X-windows world screen inspired several minimalist windows managers which usually are bungled with linux mini-distributions. Classic example of this genre is ratpoison (the name suggest that it is one of the few that not support mouse "out of principle" ;-).

Screen is portable and is an almost a standard utility on all flavors of Unix. It is often preinstalled by default in Linux distributions (for example, Suse 10). And it is easily installed in Solaris, AIX and HP-UX. For example Solaris 10 package can be found on sunfreeware.com:

screen-4.0.2-sol10-intel-local.gz Screen provides an ANSI/vt100 terminal emulator, which can multiplex up to 10 pseudo-terminals - installs in /usr/local. There are further configuration steps in the /usr/local/doc/screen/INSTALL. The etc and terminfo directories containing other relevant files are in /usr/local/doc/screen. The screen package may also require that you install the ncurses package.

Using it, you can run any number of console-based applications -- interactive command shells, curses-based applications, text editors, etc. -- within a single terminal. Many administrators who use screen open a dozen or even more sessions.

Screen can be used in two ways (you can use both local and remote screen instances simultaneously by changing control character in local to say Ctrl-\ via .screenrc, see below):

1. On local workstation (in this case you multiplex between terminal sessions opened in multiple screen windows). Many administrators use it as a terminal on steroids and have a window for each remote server they administer.
2. Remotely (in this case you need first to connect to remote machine using, say ssh or telnet, run screen and then multiplex windows on this remote machine). In this case screen provide the ability to have serial local terminal sessions via single ssh or telnet connection (for example one root window and one non-root window).

A nested master-slave invocation of screen is also possible, but it is unclear how practical it is. See Jason White page and my screenrc and my slave screenrc by Tim Chambers. Here is how he explained this method in Slashdot post:

You run an "outer" screen session (the "slave" session) that in turn runs an "inner" (or "master") session. You use the regular escape sequence (Ctrl-A d) to detach from the master, and you map Ctrl-^ to be the control key for the slave session. If you press Ctrl-^ while using screen this way, you'll see one process in the slave session. It's running ssh-agent. That's the key to using ssh with screen. The slave's only purpose is to run ssh-agent. The master runs as a child of that. Consequently, all shells in the master session are running under the ssh-agent. Just run ssh-add from any master shell, and then all shells have your ssh identity.

As we mentioned above, after starting screen you need to open windows using the command Ctrl-a c. Every window in screen is identified by a unique number. The first window is usually numbered 0 (you can change this to one if you do not like counting from zero). For each created windows screen starts default shell. After that user for example can open remote session to a particular server and rename it to simplify recognition after switching to another (an optimal typical number of windows for one screen instance is around a dozen). You can use multiple screen instances each with its own set of servers to connect (DMZ servers, database servers, etc).

Minimum set of commands necessary to use screen productively includes just five commands:

1. Ctrl-a c to create a new window (Ctrl-a N -- lists the number of the active windows)
2. Ctrl-a to switch between them (Ctrl-a n, Ctrl-a p or Ctrl-a Ctrl-a will work too providing previous and next windows, but are less convenient and there are two of them instead of one)
3. Ctrl-a " (windows) Show a list of active windows. You can navigate this list with the arrow keys (or vi-style, with j and k), and pick a window to activate by pressing Enter when it's highlighted. As this is one of the most often used commands you will be better off redefining this command as Ctrl-a space (see .screenrc examples on how to do that).
4. Ctrl-a d. detach from current session. You can also detach just by closing the terminal emulator that contains the session. Neither of these actually end your session. All they do is unbind your session from the current terminal. All of the programs you started running within screen are still running. To reattach type screen -r from the command prompt
5. Ctrl-a Ctrl-\ (quit) Kill all windows and terminate screen. You can close individual window by exiting shell with command exit.

There are also several more advanced features that can improve your productivity. Among them I would like first of all mention regions.

Regions can dramatically improves your productivity in pure command line environment. This feature turns screen in rudimentary windows manager able to create "strips" non-overlapping horizontal zones each of which corresponds to a separate shell window. To split the screen into two regions just press C-a S. The current window will split into two separate areas with the bottom area blank. To switch to the bottom part press C-a [Tab]. If you need to create a new shell in the new window, so press C-a c. If you want to see one of the existing sessions press Ctrl-a " and select the necessary session. You can create more regions by pressing C-a S again: each time the current "strip" will be split into two. Tabs display the active region shell name in its title bar. You can also switch between tabs being in a region, so e.g.: you have 4 tabs and 2 regions. Each region can display one of these tabs. A nice trick is that upper region can react on the change of the particular environment variable and display either file you are editing or the listing of the current directory. This arrangement is close for the OFM paradigm. You can change the size of each region by entering resize [size], where size is the number of lines.
The key additional feature of Screen that you need to master after mastring basic commands is split command.

Here is slightly more elaborate set of commands, which we will call Level 1 command set. It includes regions-related commands:

1. Ctrl-a A (title) Allow the user to enter a name for the current window (useful fo naming each window according to the purpose (for example each can represent a different directory).
2. Ctrl-a Ctrl-a (other) will switch you to whichever window you were using before the current one.
3. Ctrl-a S (split, capital s, please!). Screen can horizontally by split into regions(panels), each holding a different terminal. After you split the window you need to move to the new region using Ctrl-tab and create a session in it using Ctrl-a-c.
* To enter that newly created region we have to tab into it: Ctrl-a
(change focus switch from one panel to another in OFMs)
* To close a region type Ctrl-a X.
* A region with focus can be resized with Ctrl-a + or Ctrl-a - By default this changes the height by 3 lines.
* Typing Ctrl-a : puts Screen into command mode and the status line at the bottom changes. Screen now accepts direct commands and resize 24 makes the currently focused region exactly 24 lines high. Alternatively, you can specify the height to an absolute number by going into Screens command mode. Ctrl-a X removes the current region.

Note: It is easy to confuse with the lowercase Ctrl-a s which actually sends a command to freeze to the parent terminal. To reverse this effect one doesn't have to go as far as closing the terminal and re-attach again, but simply type Ctrl-a q.

4. Ctrl-a (focus) move the the next split window
5. Ctrl-a X (remove) Close current region
6. Ctrl-a N, where N is a number from 0 to 9, will switch to the corresponding window.

7. Ctrl-a w lists windows in one like with the active marked by asterism. This is a noninteractive list and you cannot navigate it. Interactive list is by default assigned to Ctrl-a "

8. Ctrl-a d. detach from current session. You can also detach just by closing the terminal emulator that contains the session. Neither of these actually end your session. All they do is unbind your session from the current terminal. All of the programs you started running within screen are still running. To reattach type screen -r

9. Screen remembers a configurable number of scrollback lines. You can access the scrollback buffer by entering "copy mode", which is accomplished by typing Ctrl-a [. You can mark text anywhere in the scrollback buffer and paste it with Ctrl-a ]. Screen is also capable of logging to files.

10. Screen has features that allow you to monitor a window for silence, useful, for example, for knowing when a compile has finished. To start or stop monitoring the current window for 30 seconds of silence, type Ctrl-a _; to start or stop monitoring a window for activity, type Ctrl-a M.

11. To close sessions you need to to close each of your screen windows. Exit whatever programs or shells each is running, and they will go away. When the last program running inside screen is exited, screen itself will go away. You can forcefully quit screen with Ctrl-a Ctrl-\.

As you progress you "screen vocabulary" extends and in some point you can switch from Level 1 command set to Level 2 command set which includes several additional useful commands (and first of all bindkey command):

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

Here are some examples of keyboard bindings:

bindkey -d # Show all of the default key bindings. The application mode entries are marked
with [A].

bindkey -k k1 select 1 # Makes the "F1" key switch to window one.

bindkey -k F1 command # Makes the F11 (not F1!) key an alternative screen escape (besides `Ctrl-a').

You can also easily change Ctrl-A (the default attention/command key sequence) to another sequence. This can be done either via putting command escape into .screenrc or via a switch when you start screen.

1. -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 «Ctrl-a» and `a', which can be specified as "-e ^Aa". When creating a screen session, this option sets the default command character. In a multi-user 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.

If you invoked screen by using "screen -e ^Ww" you can see that ^W became attention/command key combination. So ^W-? gets the Help screen. Ctrl-A and then ? no longer worked.

2. Adding co the .screenrc file using excape command is explained in the man page:

escape xy

Set the command character to x and the character generating a literal command character (by triggering the "meta" command) to y (similar to the -e option). Each argument is either a single character, a two-character sequence of the form "^x" (meaning "Ctrl-x"), a backslash followed by an octal number (specifying the ASCII code of the character), or a backslash followed by a second character, such as "\^" or "\\". The default is "^Aa".

For example if you add

escape ^\\

into the .screenrc you will see that ^\ became attention key sequence instead of default ^A.

It is also important to make screen more user friendly. In order better see what open windows you have you can do it by adding the following lines to .screenrc :

1 hardstatus alwayslastline
2 hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{=
kw}%?%-Lw%?%{r}(%{W}%n*%f %t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}
%Y-%m-%d %{W}%c %{g}]'

In his paper Screen Tips & Tricks Łukasz Olender suggests several other variants:

But so as not to get lost in these windows, I suggest you create tabs similar to those found in web browsers. You can do this by writing to your configuration file ~/.screenrc one of the following codes:

hardstatus alwayslastline
hardstatus string '%{= mK}%-Lw%{= KW}%50>%n%f* %t%{= mK}
%+Lw%< %{= kG}%-=%D %d %M %Y %c:%s%{-}'

or

hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}
(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %d/%m
%{W}%c %{g}]'

or

hardstatus alwayslastline
hardstatus string '%{gk}[ %{G}%H %{g}][%= %{wk}%?%-Lw%?%{=b kR}
(%{W}%n*%f %t%?(%u)%?%{=b kR})%{= kw}%?%+Lw%?%?%= %{g}][%{Y}%l%{g}]
%{=b C}[ %m/%d %c ]%{W}'

With the first code each tab will have a number, a name plus time and date. The second one will give you tabs that will be created in the center and the active tab will be highlighted in red and also have the hostname displayed. The last code will additionally display the processor usage. You can create your own codes but it is a rather specialized task - I refer you to the system manuals for more information.

You can combine ideas from those into you own status line that suits your need. But in any case it is important to improve visibility of windows by using this capability of screen.

Dr. Nikolai Bezroukov

Reference: http://www.softpanorama.org/Utilities/screen.shtml