systemd: run the stop-save inside the live server, not racing kill-server

The generated tmux.service ran the resurrect save script as a bare ExecStop
process (no $TMUX, so it depends on the default-socket server still being alive)
immediately before `ExecStop=tmux kill-server`, under KillMode=control-group.
On a shutdown/reboot/logout stop the server in the cgroup can be signalled while
save.sh is still dumping, so the save produces an empty file. With tmux-resurrect
promoting that empty dump to `last`, the previous good save is destroyed.

Run the save via `tmux run-shell` so it executes inside the live server (correct
socket, synchronous) and completes before kill-server; prefix both stop commands
with `-` so a stop when the server is already gone doesn't write an empty save.
Use KillMode=mixed and TimeoutStopSec so the save has room to finish during a
shutdown stop. Drop the dead RestartSec (no Restart= is set, and auto-restart is
intentionally avoided so a broken restore can't spin into a crash loop).

Note: the durable fix for the empty-save data loss belongs in tmux-resurrect's
save.sh (reject empty dumps before promoting to `last`); this change removes the
systemd path that triggers it.
This commit is contained in:
Aljoscha Gerber 2026-07-07 10:40:53 +02:00
parent 0698e8f4b1
commit d0f5454462

View file

@ -26,12 +26,18 @@ template() {
Environment=DISPLAY=:0
ExecStart=${tmux_path} ${systemd_tmux_server_start_cmd}
ExecStop=${resurrect_save_script_path}
ExecStop=${tmux_path} kill-server
KillMode=control-group
# Save the running session, then kill the server. save.sh is run via
# 'tmux run-shell' so it executes inside the live server (correct socket,
# synchronous) and finishes before kill-server; if the server is already
# gone it simply fails (leading '-') instead of writing an empty save that
# would clobber the previous good "last". KillMode=mixed plus a stop timeout
# give the save room to finish during a shutdown/reboot stop.
ExecStop=-${tmux_path} run-shell "${resurrect_save_script_path} quiet"
ExecStop=-${tmux_path} kill-server
KillMode=mixed
TimeoutStopSec=20
RestartSec=2
[Install]
WantedBy=default.target
EOF