The Moving Parts
Screen is one program, but it helps to know it as a handful of parts, because every confusion a beginner meets is one part being mistaken for another: "my session vanished" is usually a socket question, and "Screen ate my keystroke" is the command prefix doing its job.
The server and the client
When you run screen, two roles start
at once. A long-lived server process
owns your sessions and keeps them
running; it does not need you attached,
or even logged in. Your terminal runs a
client that connects to the server
and renders one session on your screen.
Detaching is just the client leaving.
The server — and everything running
inside it — carries on. Reattaching is
a new client connecting to the same
server.
This is the whole secret, and it is worth pausing on: nothing inside a session is "resumed" or "restored" when you come back. It never stopped. Screen is not a save-and-reload system; it is a waiting room where your programs sit with their terminal held open.
Sessions
A session is one run of the Screen
server, holding a set of windows and
the programs in them. You can run
several sessions at once — say one per
project — and each has a name you choose
with -S:
screen -S website
screen -S backups
screen -ls lists them, each shown with
a number (the server's process id)
before the name. Sessions are
independent: killing one leaves the
others alone, and a session outlives the
login that started it but not a reboot
of the machine.
Windows
Inside a session, a window is one virtual terminal running one program — almost always a shell, from which you run everything else. Windows are numbered from 0 and have titles. You create them (Ctrl-A c), step through them (Ctrl-A n, Ctrl-A p), jump by number (Ctrl-A 0 … 9), and list them (Ctrl-A ") so you can pick from a menu instead of remembering. Chapter 6 is about living among several windows, including several at once on screen, in regions — Screen's splits.
Each window has its own scrollback: the lines that have rolled off the top, kept in a buffer so you can scroll and copy from them later (chapter 7). The buffer's size is configurable, and it belongs to the window, not to your terminal program — which is why the history is still there when you reattach from a different terminal.
The command prefix
Nearly every key in a terminal belongs to the program running in it, so Screen needs one key of its own. By default it takes Ctrl-A: press Ctrl-A, then a letter, and the letter is a command to Screen rather than text for your shell. Ctrl-A d detaches, Ctrl-A c creates a window, Ctrl-A ? lists the keys. To send a real Ctrl-A through to a program (the shell and Emacs both want it, for "start of line"), press Ctrl-A a.
This single prefix is the part of Screen a new user feels most — it is genuinely in the way for a day, and then it is in the fingers. Chapter 8 shows how to move it to another key if Ctrl-A matters more to your other tools than to Screen.
The socket
Client and server talk through a
socket — a special file in a
directory only you can read, by default
under /run/screen/S-$USER (older
systems used /tmp). screen -ls is
really a reading of that directory,
which explains two classic mysteries:
- If the machine reboots, the socket is
gone, and
-lsreportsNo Sockets found— the sessions went with it. - If a session's server dies while the
socket remains,
-lsmay list it as (Dead ???);screen -wipeclears the corpse away.
Chapter 14 meets both. The socket's permissions are also the security model in miniature: another user on the same machine cannot attach to your session unless you deliberately open the door (chapter 9 does, carefully).
The .screenrc file
Screen reads settings from
~/.screenrc at startup: the
scrollback size, the status line, which
windows to open ready-made, the command
prefix itself. Every default in this
guide can be bent there, and chapter 8
builds one line by line. Sessions
started before a change do not pick it
up — the file is read when a session
starts, a fact chapter 14 will remind
you of when a new setting "does nothing".
Who holds what, when it breaks
- The server holds your programs. If it dies, they get the hangup signal and end; there is no undo.
- The client holds nothing but the view. Kill it, close the laptop, lose the network — the session is untouched.
- The socket holds the address. No socket, no reattaching, even if the server somehow lived.
- The scrollback holds the recent past of each window — long enough to be useful, finite enough that chapter 11 shows how to log to a real file when the past matters.