tmux: Sessions That Survive Disconnects

Keep long jobs alive after you log out, split panes, and reattach to your work from anywhere.

You start a long migration over SSH, your wifi drops, and the process dies with the connection. tmux (terminal multiplexer) solves this permanently: it runs a session on the server that keeps going whether or not you are attached.

The one concept

A tmux session lives on the server independently of your terminal. You attach to see it and detach to leave it running. Reconnect from a different machine hours later and tmux attach puts you right back where you were, mid-command.

The prefix key

Every tmux command starts with the prefix, by default Ctrl+b, then another key:

KeysAction
prefix dDetach (leave it running)
prefix cNew window
prefix % / "Split pane vertically / horizontally
prefix o / arrowsMove between panes
prefix [Scroll/copy mode (q to exit)

Why not just nohup?

nohup keeps a job alive but gives you nothing to reattach to — no scrollback, no interactivity. tmux keeps the whole live terminal, so you can watch output, answer a prompt, or run several panes side by side.

Example

Example · bash
# Start a named session for a task
tmux new -s migrate

# ...kick off the long job inside it...
./run-migration.sh

# Detach and log off safely:  press  Ctrl+b  then  d
# The migration keeps running on the server.

# Later, from any machine:
ssh web1
tmux ls
# migrate: 1 windows (created Wed Jul 15 ...) 
tmux attach -t migrate      # right back in, mid-run

# Split the current pane and open a log alongside:
#   Ctrl+b  %          -> split left/right
#   Ctrl+b  <arrow>    -> jump between panes
#   tail -f /var/log/app.log

When to use it

  • A developer starts a long test suite inside a tmux session, detaches, closes their laptop, and reattaches the next morning to find it still running.
  • A sysadmin uses tmux split panes to simultaneously tail a log file and run commands in the same terminal window.
  • A DevOps engineer creates a named tmux session called 'deploy' shared between two engineers who monitor the same deployment output.

More examples

Start, detach, and reattach

Shows the core tmux workflow: create a named session, detach with Ctrl+b d, and reattach later.

Example · bash
# Start a new named session
tmux new -s deploy

# Inside tmux, detach (keep running after logout)
# Press: Ctrl+b  then  d

# List sessions
tmux ls
# deploy: 1 windows (created Jul 16 14:00)

# Reattach
tmux attach -t deploy

Split panes for multi-view

Lists the key bindings to split the terminal into multiple panes and navigate between them.

Example · bash
# Inside tmux:
# Split horizontally (top/bottom)
# Ctrl+b  then  "

# Split vertically (left/right)
# Ctrl+b  then  %

# Navigate between panes
# Ctrl+b  then  arrow key

# Zoom into one pane (toggle)
# Ctrl+b  then  z

Pair programming with shared session

Shows two users attaching to the same tmux session for real-time collaboration; -r makes the second session read-only.

Example · bash
# User 1: create a shared session
tmux new -s shared

# User 2 (on same server): attach to the same session
tmux attach -t shared
# Both users now see the same terminal output

# Or read-only for the second user
tmux attach -t shared -r

Discussion

  • Be the first to comment on this lesson.