Skip to content

Recipes

Short, complete jobs, each one built from pieces the earlier chapters verified. Where a recipe leans on a manual page rather than the lab, it says so.

The build that survives the train

Problem: a long job on a remote machine; a journey and a flaky connection between you and it.

ssh you@far-machine
screen -S build
./run-the-long-build.sh
# Ctrl-A d, close the laptop, travel.
ssh you@far-machine
screen -dR build

The job belongs to the machine from the first command on. Chapter 10 is this recipe with the explanations left in.

Keep a real log of an overnight run

Problem: the scrollback is finite (chapter 7) and the run is not.

Start the session with logging on:

screen -dmS nightly -L -Logfile nightly.log
screen -S nightly -X stuff './nightly-job.sh
'

or, in a session already running, press Ctrl-A H in the window to toggle logging for that window (to a file named screenlog.0 in the window's working directory, by default). The log is a plain file: it grows while you are detached, it survives the session, and tail -f can watch it from anywhere. The lab verified the Ctrl-A H route end to end (transcript screen-lab/transcripts/t08-sty-and-logging.txt): output printed after the keypress is in screenlog.0, output from before it is not โ€” logging starts when asked, not retroactively. The -L/-Logfile flags are the same mechanism set at birth, taken from the manual.

Drive a detached session from a script

Problem: a scheduler or a script must put a running session through its paces. This is the lab's own method (transcripts t01 and t03):

screen -dmS job
screen -S job -X stuff './do-the-thing.sh
'
screen -S job -X hardcopy result.txt

One caution the lab earned: a command handed to -X has its $VARIABLES expanded by Screen before it is typed, so a script line containing $HOME arrives as the expanded text โ€” or as nothing, for a variable Screen's environment lacks. Type a script file's name instead of an inline command with dollars in it, as above.

One session per project, named for it

Problem: three projects, one server, and a -ls list that tells the truth.

screen -dR website
screen -dR invoices
screen -dR backups

plus the alias from chapter 10 (s website). The names are the documentation: screen -ls at any moment shows what exists, and -dR means you never have to remember whether you left one attached at the office.

The watch window

Problem: a log must be visible while you work, not instead of working.

In one session: window 0 for your shell, then Ctrl-A c and

tail -f /var/log/the-service.log

Name it with Ctrl-A A (logs), and keep a third window for the editor. Chapter 6's splits go one step further when the terminal is big enough: the log in the region below, the shell in the region above, with Ctrl-A Tab between them.

The second pair of eyes

Problem: a colleague is stuck on a machine you both can reach.

# the colleague, in their session on that machine:
screen -S debug
# you, logged in as the same user:
screen -x debug

Chapter 9 is the full story, rule included: you now hold their keyboard, so behave like a guest who has been handed one.

Audit and tidy-up day

Problem: sessions accumulate like browser tabs, and some of them are dead.

screen -ls        # what exists, and in what state
screen -wipe      # clear listings whose server is gone (Dead ???)
screen -S old -X quit   # end the ones you are done with

A session that shows (Attached) on a machine you are not sitting at is tomorrow's -dR working as designed, not a problem. A (Detached) session nobody recognises is worth reattaching to once, to see whose scene it is, before it is ended โ€” quitting is the one irreversible command in this guide.