kill session using hook and session_name variable

10 views
Skip to first unread message

Billy Bromell

unread,
Jan 6, 2021, 11:43:07 AM1/6/21
to tmux-users
Hi,

I think I've got the wrong syntax for this, any pointers would be appreciated!

```
# If pane 0 dies, kill any previous MACHINE-dead sessions, and rename this session
# from MACHINE to MACHINE-dead
set-hook -g pane-died 'if -F "#{&&:#{==:#{window_name},netkit-vm},#{==:#{pane_index},0}}" "kill-session -t #{session_name}-dead; rename-session #{session_name}-dead" "kill-pane"'
```

I'm trying to make it so that if the process in window netkit-vm, pane 0 dies, then the session gets renamed to append '-dead' to the current name (session a > session a-dead),  however it needs to kill the -dead sessions of the same name before, otherwise rename-session would fail as it would be a duplicate session.

The if statement seems to be working fine but kill session is failing (i get a message in the status bar showing the correct usage of kill-session). Are there some escape characters I'm missing for the variables?

Thanks,
Billy


Nicholas Marriott

unread,
Jan 6, 2021, 12:18:07 PM1/6/21
to Billy Bromell, tmux-users
if-shell does not expand formats in its command arguments for some reason.

Also this will not work if the old session does not exist, because the kill-session will fail so the rename-session will not run.

You need to wrap it in run-shell which does expand formats, and run them as separate commands:

set -g remain-on-exit on
set-hook -g pane-died {
    if -F "#{&&:#{==:#{window_name},netkit-vm},#{==:#{pane_index},0}}" {
        run-shell "
            tmux kill-session -t '=#{session_name}-dead'
            tmux rename-session -t '=#{session_name}' '#{session_name}-dead'
        "
    }
}

If your tmux is too old for multiline "s, then you will have to do "tmux kill-session ...; tmux rename-session ...".

If you are using a tmux which is older and does not support {}, quoting will be quite annoying. Personally I would probably put the whole hook in a shell script and just pass in #{session_name} #{window_name} and #{pane_index} as arguments:

set-hook -g pane-died 'run-shell "/path/to/my/script \"#{q:session_name}\" \"#{q:window_name}\" #{pane_index}"'

Then you can do whatever you like in the script (but be careful of races with multiple separate tmux commands).


--
You received this message because you are subscribed to the Google Groups "tmux-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tmux-users+...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/tmux-users/7dace6e6-2355-424c-bfd4-98e8b4ba15c9n%40googlegroups.com.

Billy Bromell

unread,
Jan 6, 2021, 1:07:31 PM1/6/21
to tmux-users
I don't think my tmux does support multiline quotes :( however i have it working with this:

set -g remain-on-exit on
set-hook -g pane-died {
    if -F "#{&&:#{==:#{window_name},netkit-vm},#{==:#{pane_index},0}}" {
        run-shell "tmux -L netkit has-session -t '=#{session_name}-dead' && tmux kill-session -t '=#{session_name}-dead'; tmux -L netkit rename-session -t '=#{session_name}' '#{session_name}-dead'"
    } {
        run-shell "tmux -L netkit kill-pane -t #{session_name}:#{window_name}.#{pane_index}"
    }
}

Thanks for your help!!
Reply all
Reply to author
Forward
0 new messages