Most of my work on Pis and servers happens over SSH. tmux keeps long jobs alive when the connection drops, and a small script opens the same window layout for each project. I have used tmux like this for years; this page is the whole setup.
Sessions that survive a dropped connection
The first reason I installed tmux: a long compile on a Pi over SSH, the laptop went to sleep, the SSH session closed, and the build was killed. A job that runs inside tmux on the remote machine keeps running when you disconnect, and you can attach to it again later.
tmux new -s build # start a session called "build"
# start the job, then press Ctrl-b d to detach
tmux ls # list sessions
tmux attach -t build # connect again
All keys inside tmux start with the prefix Ctrl-b. These cover most days:
| Keys | Action |
|---|---|
Ctrl-b d |
detach |
Ctrl-b c |
new window |
Ctrl-b n / Ctrl-b p |
next / previous window |
Ctrl-b % / Ctrl-b " |
split left/right, top/bottom |
Ctrl-b o |
go to the next pane |
Ctrl-b [ |
scroll mode (arrows, PgUp; q to leave) |
I keep the prefix at Ctrl-b. Many people change it to Ctrl-a, but in the shell and in Emacs that is “go
to the start of the line”, and I use it all the time.
A short config
# ~/.tmux.conf
set -g history-limit 50000
set -g base-index 1 # windows start at 1, easier to reach on the keyboard
set -g mouse on
set -g status-right "#H %H:%M" # host name in the status line
bind r source-file ~/.tmux.conf \; display "reloaded"
The host name in the status line came after I typed a command on the wrong Pi. Once was enough.
set -g mouse on needs tmux 2.1 or newer. Older versions, like the one in Debian Jessie, had several
separate options (mode-mouse, mouse-select-pane and others), and a config with the wrong ones prints
errors at start. If you copy the file to an old machine, check tmux -V first.
I keep the config small on purpose. It is one more file to copy to every machine.
One layout per project
On a server or a Pi I want the same layout every time: a window for the editor, one for a shell, one for logs.
Building it by hand after every reboot got old, so this script does it. Save it as ~/bin/tp and make it
executable:
#!/usr/bin/env bash
# tp: open or attach a tmux session for a project directory
set -euo pipefail
dir="${1:-$PWD}"
dir="$(cd "$dir" && pwd)"
name="$(basename "$dir" | tr '.:' '__')"
if ! tmux has-session -t "=$name" 2>/dev/null; then
tmux new-session -d -s "$name" -n edit -c "$dir"
tmux new-window -t "$name:" -n shell -c "$dir"
tmux new-window -t "$name:" -n logs -c "$dir"
tmux send-keys -t "$name:logs" 'journalctl -f' C-m
tmux select-window -t "$name:edit"
fi
if [ -n "${TMUX:-}" ]; then
tmux switch-client -t "$name"
else
tmux attach-session -t "$name"
fi
tp ~/projects/sensor-gateway
tp # uses the current directory
Details that matter:
-c "$dir"sets the start directory of each window. Without it, new windows start where you ran tmux.- The
=inhas-session -t "=$name"asks for an exact match. Without it tmux matches a prefix, andtp apiattaches to a session calledapi-old. That one confused me for a while. - Session names cannot contain
.or:, sotrreplaces them. - Inside tmux,
attach-sessionwould nest sessions, so the script usesswitch-clientthere. send-keys ... C-mtypes a command and presses Enter.docker compose logs -fortail -Fwork the same way asjournalctl -f.
A split or an editor in the first window are one line each:
tmux split-window -h -t "$name:shell" -c "$dir"
tmux send-keys -t "$name:edit" "${EDITOR:-vi} ." C-m
tmuxinator and tmuxp do the same with YAML files, and they are fine. I prefer a script that only needs tmux and bash. It works on a fresh Pi without installing Ruby or Python packages, and it lives in my dotfiles repository.
Things to know
- tmux inside tmux (one on the laptop, one on the server) sends the prefix to the outer one. Press it twice to reach the inner one. I stopped running tmux on the laptop.
- A session keeps the environment from when it was created. After you reconnect with a new SSH agent,
SSH_AUTH_SOCKin old windows can point to a socket that is gone, andgit pushfails in a confusing way. Open a new window, or set the variable again. - tmux keeps jobs alive when SSH drops, not when the machine reboots. Anything that must always run belongs in systemd.