First off to anticipate confusion: this is indeed asking the opposite of your run of the mill tmux question.
How can one ensure that if the SSH tunnel dies, no tmux session is leaked at all. When running multiple shells over the same SSH session, terminating the session and the tmux instance cleanly in one terminal will still cause tmux instances in other terminals to leak. This can cause all kinds of trouble if e. g. one forgot to terminate a resource consuming process that was lingering hidden in some backgrounded pane. Not cool.
I’m using tmux almost exclusively as a terminal emulator and have
little use if any for its “detach” feature. It’s not uncommon that
I have multiple tmux instances in terminals accessing the same
machine over a single SSH session. If I want to run a background
process, I ask the shell to disown
it or run it in
systemd. Unfortunately, many systems I’m working with day to day
haven’t upgraded to systemd yet and are unlikely to do that in
the near future so KillUserProcesses
is not an option.
Ideally I’d just invoke tmux with some command line switch that
prevents it from running in the background so I can alias that
to tmux
.
What SSH tunnels?
One OpenSSH connection serving multiple sessions.
What tmux session?
Separate tmux instances, one per SSH session.
What leaks?
Leaks of tmux instances. As in: SSH into a machine. Start another shell session
over the same connection. Start a tmux instance in each SSH session. You now
have two tmux instances. Stop tmux in one session (<C-d>
); then stop the SSH
connection (again <C-d>
): The other SSH session is closed but the tmux instance
running inside it is leaked with all it child processes.
Please describe: (1) what setup you have now,
Stock OpenSSH, stock tmux, some shell. Mostly reverse SSH connections that I cannot reestablish to at will, but that shouldn’t matter.
and (2) what scenario you're trying to prevent.
Processes staying alive in the other tmux session.
Note that those are different boxes being SSH’d into, so the goal is to prevent even a single tmux instance from remaining alive after the connection dies. Also, since I use split panes and backgrounded windows heavily, I really wish for all the child processes to be collected.
tmux
session? What leaks? Please describe: (1) what setup you have now, and (2) what scenario you're trying to prevent. Please skip rants aboutsystemd
,disown
, and how you think they should work. Just describe the problem.trap "kill $thepid" HUP
ortrap <however you delete a tmux session by name> HUP