tmux-continuum icon indicating copy to clipboard operation
tmux-continuum copied to clipboard

Current and last active sessions are not correctly saved and restored

Open alexghergh opened this issue 4 years ago • 0 comments

First of all, thank you very much for this piece of software, it is an actual life-saver!

Now for my issue.

As I understand it, when I power-off the computer, tmux-continuum should automatically save the sessions through tmux-resurrect (so basically there should be a last file in my default resurrect location with the active panes/windows/sessions).

This is done with a systemd unit file:

[Unit]
Description=tmux default session (detached)
Documentation=man:tmux(1)

[Service]
Type=forking
Environment=DISPLAY=:0
ExecStart=/usr/bin/tmux new-session -d

ExecStop=/home/$USER/.tmux/plugins/tmux-resurrect/scripts/save.sh
ExecStop=/usr/bin/tmux kill-server
KillMode=none

RestartSec=2

[Install]
WantedBy=default.target

So this unit file should call tmux-resurrect save script on exit (this means either computer shutdown, or a manual systemctl --user stop tmux.service.

However, it doesn't seem to work correctly. My last resurrect files look like this (notice there are no sessions correctly saved after the state at the end):

pane	main	1	:zsh	1	:*	1	:/home/$USER/.tmux/resurrect	1	zsh	:sqlite3 -batch /home/$USER/.histdb/zsh-history.db
pane	main	1	:zsh	1	:*	2	:/home/$USER	0	zsh	:
pane	main	1	:zsh	1	:*	3	:/home/$USER/.tmux/plugins/tmux-continuum	0	vim	:sqlite3 -batch /home/$USER/.histdb/zsh-history.db
vim ../tmux-resurrect/scripts/save.sh
window	main	1	1	:*	9279,212x57,0,0{106x57,0,0[106x28,0,0,14,106x28,0,29,15],105x57,107,0,16}
state		

After further investigation, it seems it has to do with how the systemd unit file calls the tmux-resurrect save script.

This save script saves sessions as:

state_format() {
	local format
	format+="state"
	format+="${delimiter}"
	format+="#{client_session}"
	format+="${delimiter}"
	format+="#{client_last_session}"
	echo "$format"
}
...
dump_state() {
	tmux display-message -p "$(state_format)"
}

Can be seen in context here.

But this does not work if not called INSIDE a tmux attached session. What I suspect is that indeed the ExecStop command in the systemd unit file does not call the save script inside the tmux session, but rather simply outside in a shell, which causes tmux display-message -p "$(state_format)" to display nothing.

Is there any way this could be solved? Maybe modifying the type of the systemd unit from forking to something else (not an expert on systemd unit files, just throwing in my 2 cents)?

alexghergh avatar Jun 13 '21 10:06 alexghergh