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
-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.
| Command | What it does |
|---|---|
screen -S work -X screen -t logs tail -f app.log | Open a new window |
screen -S work -p 0 -X title editor | Rename window 0 |
screen -S work -p logs -X kill | Close the window titled "logs" |
screen -S work -X quit | End 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:
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 on a window you know is sitting
at a prompt or waiting for exactly that input.
Logging and Snapshots
| Method | What it does |
|---|---|
screen -L | Start with logging turned on; output goes to screenlog.n (n is the window number) in the starting directory |
screen -L -Logfile build.log | Choose the log file name (newer versions) |
Ctrl-a H | Turn logging on or off for the current window |
Ctrl-a h | Save a snapshot of what's on screen now to hardcopy.n |
-X hardcopy file | Take the same snapshot from outside the session |
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.
| Keys | What it does |
|---|---|
Ctrl-a [ | Enter copy mode (also Ctrl-a Esc) |
Arrow keys, PgUp/PgDn | Move around the history (vi-style keys like k, j and / to search also work) |
Space | Mark the start of a selection, then again to mark the end and copy it |
Esc | Leave 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.
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
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.
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.
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.
Chapter 9 Quick Reference
screen -dmS name [cmd]starts a detached session; wrap the command inbash -c '...; exec bash'to keep the window open-Xsends a screen command to a session;-pselects 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) orCtrl-a Hlog output;Ctrl-a hor-X hardcopytakes a snapshot- Copy mode:
Ctrl-a [, move,Spaceto mark start and end, thenCtrl-a ]to paste; setdefscrollback ~/.screenrc(or-c file) holds settings:startup_message off,defscrollback,hardstatus, startup windows-xshares your own session; sharing with other users needsmultiuser/acladdand admin setup