Ramblings of an aging IT geek
← Ramblings of an aging IT geek
tooling

How tmux Stopped Me Losing Work to a Dropped SSH Connection

The handful of tmux habits that mean a dropped connection, a closed laptop or a reboot no longer costs me a long-running job or a carefully arranged set of panes.

A mechanical keyboard in front of a terminal

For years my relationship with long-running remote work was pure anxiety. Start a migration over SSH, then sit very still and pray the WiFi held, because the moment the connection dropped the shell died and took the job with it. I knew tmux existed. I'd even used it badly. What I hadn't done was build the small set of habits that make a dropped connection a non-event instead of a disaster.

The core idea is dead simple. tmux runs a session on the remote machine that outlives your connection to it. You attach, you do work, you detach (or your connection drops, same effect), and the session carries on without you. Reattach later and everything is exactly where you left it, mid-job, scrollback intact.

The three commands that matter

You can get a remarkable amount of value from almost nothing. These are the only commands I used for the first year.

tmux new -s deploy    # start a named session
# ... do work, then press Ctrl-b d to detach
tmux ls               # list running sessions
tmux attach -t deploy # reattach by name

Naming the session is the bit people skip, and it's the bit that pays off. tmux attach -t deploy reads like a sentence. Hunting through tmux ls for an anonymous session called 0 does not. Name them after the task and future-you will thank present-you.

A grid of code panes on a single screen

Panes, once you trust it

Once detaching stopped scaring me, I started actually using the layout. Splitting the window means I can watch logs in one pane while I work in another, all inside one session that survives a disconnect as a unit.

Ctrl-b %    split vertically
Ctrl-b "    split horizontally
Ctrl-b o    cycle between panes
Ctrl-b z    zoom the current pane to fullscreen, toggle back

That zoom binding is underrated. Splat your panes out to keep an eye on everything, then zoom into the one you're typing in when you need the space, and pop back out without disturbing the layout.

A config worth having

I keep my tmux config short on purpose. Two changes earn their keep. Turning on mouse mode so I can scroll and resize without thinking, and remapping the prefix is a matter of taste so I leave it alone, but the mouse one is universal:

# ~/.tmux.conf
set -g mouse on
set -g history-limit 50000

The larger history buffer means a job that prints a lot doesn't scroll its early output into oblivion before I get back to read it.

What it actually changed

The honest answer is that it changed my behaviour, not just my tooling. I no longer hold my breath during long jobs. I close my laptop and walk away from a running migration without a second thought, because the migration isn't running on my laptop, it's running on the server, and tmux is babysitting it. When I get the inevitable "your connection to the host has been lost" I reattach and find the job either finished or still chugging, scrollback and all.

It is not a clever piece of software in the sense of being hard to understand. It's clever in the sense of removing an entire category of small daily disasters for the cost of about four keystrokes. I should have built the habit a decade earlier. If you're still running long remote jobs in a bare shell and trusting your network, this is the one bit of tooling I'd press into your hands first.