Screen

From s5h.net

Jump to: navigation, search

Contents

overview

Screen is a great program for keeping console applications running and storing their output.

Screen is an essential part of any admins toolkit.

One of the big advantages of screen over a normal terminal is the bindkey function. This allows you to assign a series of keystrokes to a smaller sequence. This typing !at could tell screen to type sudo apache2ctl configtest && sudo apache2ctl graceful for example. This together with the scroll back function creates a very versatile application.

In the below .screenrc file the strokes !ht cause apache to run a clean and !sb is an internal screen command sequence to reset the scrollback lines.

In order to put a carriage return into a stuff command from within vim press ctrl-v followed by ctrl-m.

# skip the startup message
startup_message off

# go to home dir
chdir

# Automatically detach on hangup.
autodetach on

# Change default scrollback value for new windows
defscrollback 5000

# start with visual bell as default
vbell on
vbell_msg "bell on %t (%n)"

# look and feel
# caption always "%{= bb}%{+b w}%n %h %=%t %c"
hardstatus alwayslastline "%-Lw%{= BW}%50>%n%f* %t%{-}%+Lw%<"

activity "Activity in %t(%n)"

shelltitle "shell"

# log files somewhere sensible with timestamps
logfile logs/%Y%m%d-screenlog-%c.%n

# Run a screensaver if there's nothing happening for a while.
idle 420 eval "screen cmatrix -f -o -u 10" "idle 0"

bindkey -t !sb eval "scrollback 0" "scrollback 1000"
bindkey -t !ht stuff "sudo apache2ctl configtest && apache2ctl graceful^M"

Some of the most common screen commands I use,

command purpose
c create new window
H turn on logging
n next window
p previous window
A change window name
' jump to window number
" list windows
k kill current window

Automation

It took a while but I thought there would be a way to send some data to a pts to prevent it from timing a session out, although this path proved too complex through /dev/pts and unfruitful for me. GNU Screen does have a mechanism to do this however, though -X. For example, from cron, I've configured this, to send the .info command to a screen session:

1       1       *       *       *       /usr/local/bin.sun4/screen -rd talker -X stuff '^U.info^M'

This, tells the session named 'talker' to stuff the text '.info' into the session. Replace ^U and ^M with ctrl-v followed by u/m (carriage return character), this simulates the user clearing the line and typing .info followed by the return key.

Automation can be further enhanced though the -p argument.

Screen makes it very handy if you want to automate both the left and right hand site of an application (simulate the user and server).

scroll back

Scroll back isn't as friendly as in vim, but there is some overlap with the key strokes

keystroke action
h Move the cursor left by one character
j Move the cursor down by one line
k Move the cursor up by one line
l Move the cursor right by one character
0/^ Move to the beginning of the current line
$ Move to the end of the current line
G Moves to the specified line (defaults to the end of the buffer)
C-u Scrolls a half page up
C-b Scrolls a full page up
C-d Scrolls a half page down
C-f Scrolls the full page down

sessions

Suppose you want a set of sessions created to various hosts, one way to do that might be something along the lines of:

$ screen -d -m -S mega
$ for i in host1 host2 host3 ; do screen -rd mega -X screen -t $i ssh $i ; done;

This will create three sessions to host[1..3] with the title of host[1..3].

This is perhaps one of the most useful features that I've found with screen (besides all the other useful things of course). There is a maximum of 40 windows allowed with the default build of screen. If you want to change this, reconfigure your screen build with -DMAXWIN=255, or edit the config and set the MAXWIN there.

When using the screen command it might be worth noting the following very useful parameters

parameter description
-T Sets the TERM environment, such as vt100, xterm, xterm-color and xterm-256color. This is highly useful if the default TERM does not support the bells and whistles of xterm
-M Monitors the window for activity
-t Sets the title of the window


pre-selection

In the previous example we have a number of screen windows with title host1. Should you wish to select for example host1 and list a file, then write something to the terminal you might do something like this:

$ screen -rd mega -p host1 -X stuff 'ls -al ~/.screenrc^Mecho joy^M'

I hope this gives you an idea of how even more useful screen can be than it first appears!

I often use pre-selection when the window list cannot be searched through, for example rather than scan through 40 window titles with my eye balls I can drop out of the screen session and use screen -r -p name to jump to the window title name.

Personal tools
Google AdSense