Screen

From LQWiki
Jump to: navigation, search

GNU screen is a terminal multiplexer, i.e. it lets you run many command-line applications from one console or terminal emulator window.

OverView

Screen allows you to

  • detach from a program, leave it running, and attach from another computer. You will not lose any work.
  • work collaboratively on a console, every connected person can type or watch the other typing. You can teach others or give support.
  • log your session

Another underrated benefit is that you can switch between terminals without resorting to the mouse.

If you want to attach (screen -x) and there are multiple screen sessions available, the program will then list the available screen sessions. For example:

   There are screens on:
        2463.pts-2.atreus       (Attached)
        11068.lab       (Attached)
2 Sockets in /tmp/screens/S-keithh.

In this case, the user can explicitly specify the desired session using an unambiguous substring of the session name. In the above example, screen -x 11068 and screen -x lab are equivalent and both users will now share the same session. Because each session can contain multiple PTYs, each user can independently switch between the them. They can also share the same PTY in which case they will each see the same display and can each type commands.

Using Screen

To start a new screen, open A console and enter:

screen

If you lose your connection, you can resume your screen by calling

screen -r

If you want to share a screen session with another user, let him log in as your user and call

screen -x

Screen can be controlled while it is running by prefacing commands with the command-key character. By default, this is Control-A. For instance, to view the help screen enter Ctrl-a ? The detach command is Control-a Control-D.

Control-a, Control-c creates a new terminal screen

Control-a, " shows a list of terminal screens

Control-a, n switch to the next screen

Control-a, p switch to the previous screen

Control-a, a switches to the most-recently-used screen

Control-a, d kills the actual shell

Control-a, c creates a new shell

Control-a, A renames the current shell

Control-a, H logs your session to a plain-text file

Customizing Screen

The man page for screen details a large number of options but the following examples will illustrate many of the more useful features. By default, screen checks for a ~/.screenrc file and failing that the /etc/screenrc file. Here is my default screenrc:

defflow auto

# Scrollback buffer size in lines
defscrollback 5000

# modify the termcap/terminfo when I'm using XTerm.
terminfo xterm* LP

# Support alternate screens so that, for example, when you 
# quit out of vi, the display is redrawn as it was before vi
# redrew the full screen.
altscreen on

# don't display the copyright page
startup_message off

# detach on hangup - if my dial-up session fails, screen will simply
# detach and let me re re-attach later.
autodetach on

msgwait 2 # 1 second messages

# Start a number of initial shells and give them titles which show up in the status-line
screen -t CCase    0 bash
screen -t Sun1     1 bash
screen -t Sun2     2 bash
screen -t CCase    3 bash
screen -t Rich     6 bash
screen -t Pine     7 bash
screen -t Pine     8 bash
screen -t News     9 bash
screen

# remove some stupid / dangerous key bindings
bind k
bind W
bind ^k
bind .
bind ^\
bind \\
bind ^h
bind h
#make them better
bind 'K' kill
#bind 'I' login on
#bind 'O' login off
#bind '}' history
bind 'W' windowlist

I can create an alternate configuration file as follows:

...
sessionname lab # Call this session "lab"

caption always # Leave the status line on permanently
caption string "LAB %t - %w"

msgwait 2 # 1 second messages
screen -t CP       0 bash
screen -t CAC      1 bash
screen -t FTP      2 bash
screen -t xterm    3 bash
screen
...

This can be made easier to use by defining an alias for the command:

  • screen -d -R lab -t LAB -c ~/.screenrc-lab

The -d -R lab option means detach and resume the session named lab and if no such session exists then create it.

The -t LAB option simply puts a title on the status line.

And -c ~/.screenrc-lab specifies the alternate configuration file to use.

External links

Alternatives

The programs kibitz and script are similar and worth looking at.