How Terminal Multiplexers Keep Sessions Alive in Linux
A terminal multiplexer, such as tmux or GNU Screen, keeps sessions alive in Linux by decoupling user-facing terminal interfaces from the background processes executing the actual work. Through a client-server architecture, multiplexers manage independent pseudoterminal (PTY) pairs that persist in the system's memory even when an SSH connection drops or a local terminal emulator window closes. This design isolates running programs from standard termination signals, allowing users to safely disconnect and reattach to their active environments at any time.
The Default Behavior: Why Standard Sessions Die
In a conventional Linux terminal session, programs run as child
processes of the shell, which in turn is a child of the terminal
emulator or the SSH daemon (sshd).
When the connection closes or the window is terminated:
- The controlling terminal disconnects.
- The operating system sends a Hangup signal (
SIGHUP) to the controlling process (the shell). - The shell forwards the
SIGHUPsignal to all background and foreground child processes attached to that session. - By default, processes that do not explicitly catch or ignore
SIGHUPterminate immediately.
The Client-Server Architecture
Terminal multiplexers solve this problem by splitting the terminal environment into two distinct components: a client and a server.
- The Server Daemon: When a multiplexer starts, it
spawns a background daemon process. This server process runs
independently of the terminal that launched it, adopting
systemd(PID 1) or an init system as its parent if necessary. The server holds the state of all windows, panes, environment variables, and active processes. - The Client Process: The client is the lightweight
interface that interacts with the user's physical display and keyboard.
It connects to the server via an inter-process communication (IPC)
channel, typically a UNIX domain socket located in
/tmpor/run.
Decoupling via Pseudoterminals (PTYs)
Linux applications expect a terminal device to handle standard input
(stdin), standard output (stdout), and
standard error (stderr). Terminal multiplexers fulfill this
requirement using pseudoterminals (PTYs).
Each pane or window inside a multiplexer is allocated its own master/slave PTY pair:
- The multiplexer server opens the master side of the PTY.
- The child process (e.g., a Bash shell, a Python script, or a compiler) attaches to the slave side.
Because the running program communicates exclusively with the multiplexer server through this virtual interface, it has no direct link to the user's SSH connection or graphical terminal window.
SIGHUP Isolation and Detachment
When an SSH connection breaks or the user manually detaches:
- The SSH daemon or window manager terminates the multiplexer client.
- A
SIGHUPsignal is delivered solely to that client process. - The multiplexer server detects that the client has disconnected via the UNIX domain socket, but the server itself remains active in the background.
- Because the child processes are attached to the server's PTYs—not
the dead SSH session—no
SIGHUPis ever propagated to them. They continue running unaffected.
Reattachment
When a user reconnects and issues a reattach command (such as
tmux attach), a new client process initializes. This client
connects to the existing server via the UNIX domain socket. The server
queries its internal state, redraws the terminal buffers onto the new
client screen, and routes input and output between the user and the
persistently running PTYs.