Terminal multiplexers — why long-running sessions need tmux or screen
Closing your laptop, or an SSH connection dropping, kills every program running in that terminal session by default — a multiplexer is the standard fix, and the reason a long-running script survives a dropped connection at all.
Beginner
2 min read
The problem: a terminal session dies with its connection
Start a long-running script over SSH, then close your laptop lid or lose
Wi-Fi for a second, and by default the SSH connection drops — and every
process that was running inside that terminal session dies with it. For a
quick command this doesn't matter; for a deployment script, a long data
import, or a training job that takes hours, it's a real, recurring problem.
The fix: a session that outlives the connection to it
A terminal multiplexer (tmux or the older screen) runs a persistent
session on the server itself, independent of any specific SSH connection.
You attach to that session to interact with it and detach to
disconnect without killing anything running inside — the session, and every
program in it, keeps running on the server regardless of whether anyone is
currently connected to look at it.
The minimum tmux you need
# Start a new named session$ tmux new -s deploy# ... run your long command inside it ...$ ./run-migration.sh# Detach without stopping anything: Ctrl+b, then d# (Ctrl+b is tmux's "prefix" key — press it, release, then press the next key)# Later, from anywhere (even a fresh SSH connection): reattach$ tmux attach -t deploy# List every running session$ tmux ls
Once detached, closing your laptop or losing your connection has zero effect
on run-migration.sh — it keeps running on the server exactly as if you
were still watching it, and reattaching later drops you right back into the
same terminal output, mid-command if it's still going.
Why this matters day to day
Any process expected to run longer than a single sitting — a deployment, a
long build, a database migration, monitoring a live log during an incident —
is safer run inside a multiplexer session specifically so a dropped
connection (which will eventually happen) doesn't also kill the work.
Beyond that core use case, tmux's ability to split one terminal into
multiple panes is also a genuinely common way experienced engineers manage
several related terminal views (logs in one pane, a shell in another)
without needing several separate terminal windows.