>_ PocketShell Sign in

Stop losing work when SSH drops: tmux sessions that survive anything

It's a rite of passage every systems admin knows. You're 40 minutes into a build, your laptop suspends or the café Wi-Fi hiccups, and everything in the SSH session is gone. The process died with the connection, because it was a child of your login shell.

tmux breaks that dependency: your programs run inside a session that lives on the server, and the SSH connection is just a window into it. Close the window, on purpose or by accident, and the session keeps going.

The five commands you actually need

Run these five and you have the whole workflow:

tmux new -s deploy     # start a named session
tmux detach            # or Ctrl+b, then d — session keeps running
tmux ls                # list sessions on this server
tmux attach -t deploy  # reattach from anywhere
tmux kill-session -t deploy

Detach before you close the laptop, or don't: a dropped connection counts as a detach once the server notices. Go home and run tmux attach -t deploy from any machine. Your scrollback, panes, and running processes are exactly where you left them.

Reattach automatically on connect

tmux means a disconnect costs you one command instead of your work. Make that command zero, too.

This in ~/.bashrc on the server drops every interactive login straight into a session named main, reattaching when it already exists:

if command -v tmux >/dev/null && [ -z "$TMUX" ]; then
    tmux attach -t main 2>/dev/null || tmux new -s main
fi

Two details keep this from biting you. Place it below the interactivity guard at the top of most default .bashrc files (the case $- in *i* line), so non-interactive sessions like scp, rsync, and git over SSH never trigger it. The $TMUX check stops shells opened inside tmux from nesting sessions. If you would rather not touch .bashrc at all, tmux new -A -s main attaches to main or creates it.

Automatic reattach only helps if you notice the drop. These settings in ~/.ssh/config on your laptop make a dead connection fail fast instead of hanging forever.

The full tour of the ~/.ssh/config file covers where this belongs:

Host *
    ServerAliveInterval 30
    ServerAliveCountMax 4

The client now sends a probe every 30 seconds and declares the connection dead after 4 unanswered probes (about 2 minutes) instead of never. You find out quickly, reconnect, and main is still there.

Long-running agents and builds

tmux has quietly become the standard way to run terminal AI agents (Claude Code, Codex CLI) on a remote box. Start the agent in a named session, detach, and check in from anywhere. The same approach covers rsync of a big dataset, database migrations, and compile farms. It's the same for anything you'd hate to restart because of a train tunnel.

Two tips for this style of work:

  • Name sessions by task (agent, migrate, logs), not by date. tmux ls becomes a to-do list of what's still running.
  • Use panes for supervision. Ctrl+b % splits the window into side-by-side panes, so the agent runs in one and tail -f on its logs in the other. The layout is part of the session, so it's still there when you reattach.

tmux vs screen

GNU screen does the same job and you'll meet it on older servers. tmux is the default choice in 2026. It has saner keybindings, status bar customization, and scripting via tmux send-keys (handy for kicking off jobs from cron). Learn tmux, and read screen's man page the one time you SSH into a fossil.

One more way this pays off

Attaching to a tmux session only needs a terminal, not your terminal. That's the idea behind PocketShell: your SSH hosts are in your account, and a browser tab on any machine is a real terminal to them.

We also stopped leaning on tmux for agent work ourselves. PocketShell's session layer is aplexer, an agent-first multiplexer. Every session has a workspace, a tag, and an engine, and a list shows whether Claude Code, Codex, or OpenCode is running inside and whether it's working or waiting. For agent work, that beats remembering which pane held which job. Tmux still wins for plain shell work, so this page stays your reference there.

To learn more

Keep going with these: