Screen: A SysAdmin's PowerTool

by Ostatic Staff - Jul. 24, 2010

A major power tool that I left out of my sysadmin's toolbox article is GNU Screen. Screen has been an absolute lifesaver in the past, especially because I sometimes work remotely over wifi connections that might not always be the most reliable. Screen is a session manager for shells, a way to run and preserve multiple shells without actually having to be connected to the server. The most common way I use screen is over ssh. When connected to a server over ssh, the bash session is run over that connection, so any programs or scripts that are run are also dependent on the ssh session being active. However, when screen is run, it runs in the background, separate from the ssh session. That means screen lets your wifi connection die in the middle of running a script without the script dying as well.

Consider this scenario: the filesystem on a server is filling up because one of the apps creates text files that can get to 10Gig in size. From time to time I need to gzip some of the older files to save space, but opening an ssh session to that server and running the gzip means I'd need to maintain that connection for hours, enter screen. When I log in, instead of running gzip I run screen first, and then run gzip inside of screen. I can then press CTRL-A to get to screen's command mode, and then CTRL-D to detach from screen and log off. I can log in from home later, and run screen -r to reattach to check the progress of the command.

Screen has lots of other tricks up its sleeve too. Pressing CTRL-A followed by CTRL-C creates a new shell in the same screen session, CTRL-P goes to the previous shell while CTRL-N goes to the next shell. There are multiple settings for display options, like setting up “tabs” at the bottom of the terminal window. I use a slightly modified config file, .screenrc, that I found online when looking for examples ages ago. If anyone recognizes it please let me know in the comments where to post attribution.

Screen is a great addition to the toolbox, and another example of how seemingly simple tools can have a great affect on workflow. Do you have any favorite screen tricks, or a new program that can replace screen? Let me know in the comments!

My .screenrc file:

 # For a complete list of available commands, see http://bit.ly/jLtj

# Message to display in the status line when activity is detected in a
# monitored window.
activity              "activity in %n (%t) [%w:%s]~"

# Detach session on hangup instead of terminating screen completely.
autodetach            on              # default: on

# When a bell character is sent to a background window, screen displays a
# notification in the message line. The notification message can be re-defined
# by this command.
bell_msg              "bell     in %n (%t) [%w:%s]~"

# This command controls the display of the window captions. Normally a caption
# is only used if more than one window is shown on the display.
caption               always          "%{= kw}%?%-Lw%?%{+b kw}%n*%t%f %?(%u)%?%{= kw}%?%+Lw%?"

# Select line break behavior for copying.
crlf                  off             # default: off

# Select default utmp logging behavior.
#deflogin              off             # default: on

# Set default lines of scrollback.
defscrollback         3000            # default: 100

# If set to 'on', screen will append to the 'hardcopy.n' files created by the
# command hardcopy; otherwise, these files are overwritten each time.
hardcopy_append       on              # default: off

# This command configures the use and emulation of the terminal's hardstatus
# line. The type 'lastline' will reserve the last line of the display for the
# hardstatus. Prepending the word 'always' will force screen to use the type
# even if the terminal supports a hardstatus line.
hardstatus            alwayslastline  "%{+b kr}[ %H ] %{ky} Load: %l %-=%{kb} %c  %Y.%m.%d"
msgwait               15

# Set message displayed on pow_detach (when HUP is sent to screen's parent
# process).
pow_detach_msg        "BYE"

# Set the default program for new windows.
shell                zsh

# Default timeout to trigger an inactivity notify.
silencewait           30              # default: 30

# Change text highlighting. See http://bit.ly/11RDGZ
sorendition           gK

# Do NOT display copyright notice on startup.
startup_message       off             # default: on

# Set $TERM for new windows. I have more luck with 'linux' than Terminal's
# default 'xterm-color' (^H problems). Comment out to use the default.
term                  linux

# Tweak termcap, terminfo, and termcapinfo  entries for best performance.
termcap               linux           'AF=\E[3%dm:AB=\E[4%dm'
termcap               xterm-color     'AF=\E[3%dm:AB=\E[4%dm'
terminfo              linux           'AF=\E[3%p1%dm:AB=\E[4%p1%dm'
terminfo              xterm-color     'AF=\E[3%p1%dm:AB=\E[4%p1%dm'

# Allow xterm / Terminal scrollbars to access the scrollback buffer. This
# enables the behavior you'd expect, instead of losing the content that scrolls
# out of the window.
termcapinfo           linux           ti@:te@
termcapinfo           xterm-color     ti@:te@

# Use visual bell instead of audio bell.
vbell                 off              # default: ???

#################################################
#
# Default windows
#

screen -t "Shell" 0
screen -t "Mail" 1