devutils/ATFQ/book ATFQ git

Part III · Shell and commands

29.4 tmux: sessions that survive a lost connection

alsotmuxscreenterminal multiplexerdetachreattachtmux attachnohupdisownssh disconnectmosh

TL;DR

  • Closing an SSH connection hangs up its shell, and the hang-up signal stops what you started there.
  • tmux runs your shells in a server that keeps going: detach, log out, come back later, attach, and everything is where you left it.
  • It also splits one terminal into windows and panes.
tmux new -s work          # start a session named work
                          # Ctrl-b d: detach
tmux ls                   # sessions still running
tmux attach -t work

Pitfalls

Deep dive 2 questions

Why does logging out kill my processes?

When the connection closes, the terminal goes away and the kernel sends SIGHUP to the session's processes; most stop on it. tmux's server isn't attached to your SSH terminal: it owns its own pseudo-terminals and only displays them in yours, so losing yours changes nothing for it.

tmux or screen?

GNU screen is older and found everywhere; tmux has a cleaner config and scripting (tmux send-keys, tmux split-window), and is the more common choice today. Both detach and reattach the same way.

Commands
tmux nohup
Paths
~/.tmux.conf
See also
16.2 Signals
29.1 SSH keys and the agent