Skip to content

Instantly share code, notes, and snippets.

@jonleopard
Created December 30, 2025 02:13
Show Gist options
  • Select an option

  • Save jonleopard/a736acbf3e00c4a2f6ad64846ddc5261 to your computer and use it in GitHub Desktop.

Select an option

Save jonleopard/a736acbf3e00c4a2f6ad64846ddc5261 to your computer and use it in GitHub Desktop.

Keeping Long-Running Terminal Sessions Alive Over SSH

The Problem

When you're connected to a remote server via SSH and running a long-running process (like Claude Code), your work is tied to that SSH connection. If the connection drops — due to network instability, laptop sleep, VPN hiccups, or anything else — the process terminates immediately.

This means:

  • Any in-progress work is lost
  • You have to start over
  • Unreliable connections make remote work frustrating

The Solution: tmux

tmux (terminal multiplexer) runs sessions on the server itself, independent of your SSH connection. When your connection drops, the tmux session keeps running. You simply reconnect and pick up where you left off.

Think of it like this:

  • Without tmux: Your terminal is a window into the server. Close the window, the work stops.
  • With tmux: Your terminal is a window into a persistent session on the server. Close the window, the session continues. Open a new window, you see the same session.

Installation

tmux only needs to be installed on the remote server (not your local machine).

sudo apt update && sudo apt install tmux

Basic Usage

Starting a Session

# Create a named session
tmux new -s work

Running Your Work

Once inside tmux, run your commands normally:

claude

Detaching (Intentionally Leaving)

Press Ctrl+b then d to detach. The session keeps running.

Reattaching After Disconnection

# Reconnect via SSH first, then:
tmux attach -t work

Other Useful Commands

Command Description
tmux ls List all sessions
tmux attach -t <name> Attach to a session
tmux kill-session -t <name> End a session

Quick Reference Workflow

1. SSH into server
2. tmux new -s claude
3. Run claude (or any long-running process)
4. [connection drops]
5. SSH back into server
6. tmux attach -t claude
7. Continue where you left off

Optional: Increase Scrollback Buffer

Add this to ~/.tmux.conf on the server to keep more history:

set -g history-limit 50000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment