I'm using Gnome Terminal and tmux and I'd like that all tmux sessions were closed when I close the Gnome Terminal, is that possible? I checked the Arch Wiki article on tmux and found some relevant code, but none of them did what I want. Right now, if I close and open the Gnome Terminal I get the the session I had, exactly how it was right before I closed the terminal with all panes and programs running. What I want is that all sessions are killed when I close Gnome Terminal.
1 Answer
Option 1: use .bash_logout
In your .bash_logout
file you can add tmux kill-server
. This won't work if you aren't in a login shell, or if the shell is killed with SIGHUP.
Option 2: Use bash traps.
This option is probably more robust. Put the following code in your .bash_profile
(assuming you use bash).
function close_tmux
{
tmux kill-server
}
trap close_tmux EXIT
Links to Additional Resources On Traps
Some background on trap
statements
A blog post with plenty of trap
examples
-
Would
.bash_logout
be called if the shell session is terminated trough closing the terminal window?– Kusalananda ♦Commented Jul 25, 2018 at 18:44 -
1most likely not - I edited my answer on how to approach this Commented Jul 26, 2018 at 14:44
tmux
doesn't have any particular association with the terminal from which it was opened. It's a server and designed to act like any other server/daemon...it continues to run in the background. This allows one to attach/detach other terminals at will. So unless you only ever have one terminal window open you'll want to be careful about killingtmux
when you close a terminal.