Automating Screen

GNU Screen & Linux Job Control

Chapter 9 ยท Automating Screen

Everything so far has been done by hand, one keystroke at a time. Screen can also be driven from scripts and from its configuration file: starting sessions in the background, typing commands into windows, logging what they print, and setting up the same layout every time you start. This chapter also covers scrollback and copying text, which make long-running output much easier to work with.

Starting Sessions Detached: -dm

# New detached session called "build", running a shell screen -dmS build # New detached session running one command (ends when the command ends) screen -dmS backup rsync -a /data /mnt/backup # Keep the window open after the command, by running it inside a shell screen -dmS backup bash -c 'rsync -a /data /mnt/backup; exec bash'

-d -m creates a session without attaching to it, so it works from scripts, cron jobs and remote commands such as ssh server 'screen -dmS job ./run.sh'. The third form is useful when you want to reattach later and read the final output: without it, the window, and the session, vanish as soon as the command finishes.

Sending Commands to a Session: -X and -p

-X sends one of screen's own commands to a running session (Chapters 3 and 8 used quit and screen). -p picks which window it applies to, by number or title.

CommandWhat it does
screen -S work -X screen -t logs tail -f app.logOpen a new window
screen -S work -p 0 -X title editorRename window 0
screen -S work -p logs -X killClose the window titled "logs"
screen -S work -X quitEnd the whole session

Typing Into a Window: stuff

The stuff command puts text into a window's input, exactly as if someone had typed it. Add a newline to press Enter:

# Run "uptime" in window 0 of the session "work" screen -S work -p 0 -X stuff $'uptime\n' # Answer a prompt from a program that's waiting in window "install" screen -S work -p install -X stuff $'y\n'

The $'...\n' is bash syntax for a string containing a real newline. This is the one way to add a job to an existing window, rather than a new one: type the command into its shell.

stuff types blindly
Screen doesn't check what the window is doing. If the shell is busy, or a different program is in the foreground, your text goes to that program instead. Only use stuff on a window you know is sitting at a prompt or waiting for exactly that input.

Logging and Snapshots

MethodWhat it does
screen -LStart with logging turned on; output goes to screenlog.n (n is the window number) in the starting directory
screen -L -Logfile build.logChoose the log file name (newer versions)
Ctrl-a HTurn logging on or off for the current window
Ctrl-a hSave a snapshot of what's on screen now to hardcopy.n
-X hardcopy fileTake the same snapshot from outside the session
Check on a job without attaching
screen -S backup -p 0 -X hardcopy /tmp/backup.txt followed by cat /tmp/backup.txt shows you exactly what that window currently displays — handy in a monitoring script.

Scrollback and Copy Mode

Inside screen your terminal's own scroll bar usually doesn't show earlier output, because screen redraws the display itself. Screen keeps its own history, called scrollback, reached through copy mode.

KeysWhat it does
Ctrl-a [Enter copy mode (also Ctrl-a Esc)
Arrow keys, PgUp/PgDnMove around the history (vi-style keys like k, j and / to search also work)
SpaceMark the start of a selection, then again to mark the end and copy it
EscLeave copy mode without copying
Ctrl-a ]Paste the copied text into the current window

The default history is short. Set a bigger one in your configuration (below) with defscrollback, or for the current window with Ctrl-a :scrollback 10000.

The .screenrc File

Screen reads commands from ~/.screenrc when it starts (or from another file given with -c). A system-wide file, usually /etc/screenrc, is read first. Any command you can type after Ctrl-a : can go in it.

# ~/.screenrc # Skip the copyright page on startup startup_message off # Keep 10,000 lines of history per window defscrollback 10000 # Always show a status line at the bottom: host, then the window list, # with the current window in brackets hardstatus alwayslastline "%H | %-w[%n %t]%+w" # Open three named windows every time a new session starts screen -t shell 0 screen -t logs 1 tail -f /var/log/syslog screen -t top 2 top select 0
The status line solves "which window am I in?"
A permanent status line showing the window list is the single most useful setting for everyday use: you always know which window you're in, and that you're inside screen at all.
Test changes safely
Try a new configuration with screen -c ~/test.screenrc -S test before replacing your real ~/.screenrc. A mistake in the file can leave every new session starting in an odd state.

Sharing a Session

As Chapter 2 showed, screen -x lets you attach to your own session from two terminals at once, which is useful for showing someone your work over a shared login. Letting a different user attach needs screen's multiuser on and acladd commands, and on most systems also needs screen to be installed with special permissions. That's a security-sensitive setup best left to an administrator.

Hands-On Exercises

Exercise 1

Write a short shell script, start-backup.sh, that starts a detached, logged screen session called backup running a (harmless) command such as for i in 1 2 3; do date; sleep 20; done, and keeps the window open afterwards. Then check on it without attaching.

๐Ÿ“„ View solution
Exercise 2

From outside a detached session called work, run ls -l ~ in its window 0, then capture what the window shows into a file and display it — without ever attaching.

๐Ÿ“„ View solution
Exercise 3

Write a ~/.screenrc that turns off the startup message, keeps 5,000 lines of scrollback, shows a status line, and opens two titled windows. Test it with -c first, then use copy mode to copy a line from one window and paste it into the other.

๐Ÿ“„ View solution

Chapter 9 Quick Reference

  • screen -dmS name [cmd] starts a detached session; wrap the command in bash -c '...; exec bash' to keep the window open
  • -X sends a screen command to a session; -p selects the window by number or title
  • -X stuff $'cmd\n' types a command into a window — only into one waiting at a prompt
  • -L (and -Logfile) or Ctrl-a H log output; Ctrl-a h or -X hardcopy takes a snapshot
  • Copy mode: Ctrl-a [, move, Space to mark start and end, then Ctrl-a ] to paste; set defscrollback
  • ~/.screenrc (or -c file) holds settings: startup_message off, defscrollback, hardstatus, startup windows
  • -x shares your own session; sharing with other users needs multiuser/acladd and admin setup