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:

  1. The controlling terminal disconnects.
  2. The operating system sends a Hangup signal (SIGHUP) to the controlling process (the shell).
  3. The shell forwards the SIGHUP signal to all background and foreground child processes attached to that session.
  4. By default, processes that do not explicitly catch or ignore SIGHUP terminate immediately.

The Client-Server Architecture

Terminal multiplexers solve this problem by splitting the terminal environment into two distinct components: a client and a server.

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:

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:

  1. The SSH daemon or window manager terminates the multiplexer client.
  2. A SIGHUP signal is delivered solely to that client process.
  3. The multiplexer server detects that the client has disconnected via the UNIX domain socket, but the server itself remains active in the background.
  4. Because the child processes are attached to the server's PTYs—not the dead SSH session—no SIGHUP is 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.