Sessions over SSH
This chapter delivers the pattern the whole guide has been walking toward: your work lives on another machine, your connection to it is allowed to die, and neither fact can hurt the other. Screen on the far end of an SSH connection is the classic shape of remote work, and with the pieces from chapters 4 to 6 it is one command deep.
The pattern
Log in, and step straight into a named session:
ssh you@far-machine
screen -S work
Work. When the day or the connection ends, detach (Ctrl-A d) or simply close the laptop — a dead connection detaches you just as completely, from the session's point of view: SSH dying closes your client, and chapter 2 established what the client holds. Nothing.
Next time, from any machine, log in and take the session back:
ssh you@far-machine
screen -dR work
The one command worth memorising
screen -dR work is three decisions
folded into one line, and it is the
form most daily users end up aliasing:
- -R: reattach a session if one
exists; if the named one does not,
create it. (Plain
-rrefuses when there is nothing to reattach.) - -d: if the session is attached somewhere else — the terminal you forgot at the office — detach it there first, politely, and bring it here.
So -dR means "give me work, from
wherever it currently lives, or start
it if it lives nowhere". Run it on
every login and the question "did I
leave something running, and where?"
stops needing an answer. A small shell
function makes it a reflex:
s() { screen -dR "${1:-main}"; }
after which s joins your default
session and s website the named one.
When the connection actually drops
The honest sequence, because it will happen to you mid-sentence:
- The terminal freezes or the SSH client reports a broken pipe. Your last keystrokes may or may not have arrived; assume the partial line is lost.
- The session notices only that its client vanished. The shell and its programs run on. A build at 90% stays at 90% and finishes.
- You reconnect — the same network
healed, a different network, a
different computer entirely — and
screen -dR workputs the finished build's output in front of you, scrollback and all (chapter 7).
Everything on the Screen side of that
story is what this guide's lab verified:
transcript t01 is a program printing,
unwatched, into a detached session, and
t02 detaches and reattaches for real.
The SSH hop itself the lab did not run
— the sandbox has no SSH server — and
criteria.md labels it documented
accordingly. It is also the least
surprising hop in computing: SSH
carries a terminal to the far machine,
and on the far machine everything is
chapters 4 to 6, unchanged.
Two refinements
Know whether you are inside.
Screen sets the environment variable
$STY in every window — the
session's full pid.name. echo $STY
inside a window answers with the
session you are in (the lab checked
this from outside and in; transcript
screen-lab/transcripts/t08-sty-and-logging.txt),
and an empty answer means bare shell.
Scripts and shell prompts use it to
behave differently inside Screen, and
you will use it the first time nested
sessions (below) confuse you.
Nested sessions. SSH from inside a
Screen window into another machine and
start Screen there, and you have a
session inside a session. Both want
Ctrl-A. The inner one gets your prefix
only when you double it: Ctrl-A a d
sends the prefix through the outer
Screen (chapter 2's Ctrl-A a) so the
inner one sees its detach. It works,
it is occasionally necessary, and it is
confusing exactly as often as you would
expect — which is why echo $STY on
both sides earns its keep.
Check
From a second login to the same
machine — a second terminal will do —
run screen -dR work against a session
you left attached in the first. The
first terminal is politely detached,
the second takes the session over, and
nothing inside it so much as flickered.