Terminal Multiplexers
tmux (and the older screen) run a persistent terminal session on the server itself, independent of any one SSH connection - detach from work in progress, log out, and reattach later to find everything exactly as you left it.
An SSH connection dying mid-task - a dropped Wi-Fi connection, a closed
laptop lid - normally takes everything running in that shell down with
it (see Job Control for why: it's SIGHUP).
A terminal multiplexer runs the actual session on the remote machine, so
the SSH connection becomes just a window onto it rather than the thing
keeping it alive.
The core idea: sessions survive disconnection
tmux new -s deploy # start a new named session
# ...work happens here...
# Ctrl+B, D # detach - session keeps running in the background
tmux attach -t deploy # reattach later, from any SSH connection, even a different one
tmux ls # list all running sessions
Detaching isn't the same as closing the terminal - the session and
everything running inside it (a build, a long-running script, an
interactive top) keeps going on the server whether or not anything is
attached to watch it.
tmux: sessions, windows, and panes
tmux nests three levels: a session (the persistent container) holds one or more windows (like browser tabs), each of which can be split into multiple panes (side-by-side or stacked terminals).
All tmux keybindings start with a prefix - Ctrl+B by default, held
and released before the next key:
Keys (after Ctrl+B) |
Action |
|---|---|
D |
Detach from the session |
C |
Create a new window |
N / P |
Next / previous window |
0-9 |
Jump to window number |
% |
Split pane vertically |
" |
Split pane horizontally |
| Arrow keys | Move between panes |
[ |
Enter copy mode (scroll back, select text with keyboard) |
tmux new -s work -n editor # new session, first window named "editor"
tmux new-window -n logs # add another window, from inside tmux
tmux rename-session -t work deploy # rename a session
tmux kill-session -t deploy # end a session and everything running in it
Configuration
~/.tmux.conf
Common tweaks: remapping the prefix key (Ctrl+A is a popular
alternative, matching screen's default), enabling mouse support
(set -g mouse on), and increasing scrollback history. Reload an edited
config without restarting tmux with Ctrl+B then :source-file ~/.tmux.conf.
screen: the older alternative
screen predates tmux and is still preinstalled on some systems where
tmux isn't. The core workflow is the same shape, different keys:
screen -S deploy # start a named session
# Ctrl+A, D # detach
screen -r deploy # reattach
screen -ls # list sessions
Functionally, tmux has mostly superseded screen - better pane
splitting, scripting support, and active development - but screen's
one advantage is being present by default on more minimal systems, so
it's worth knowing the basics even if tmux is the daily driver.
Not a replacement for nohup/disown
A multiplexer and nohup/disown (see Job Control)
solve overlapping but distinct problems: nohup keeps a single command
running after disconnect, but gives no way back into its output. A
multiplexer keeps an entire interactive session - including the ability
to look at it again later - which is why it's the standard choice for
anything you'll want to check back in on, rather than fire-and-forget.