2

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.

  • Are you sure this would be done in your bashrc? The question might be better received if you just ask how to close all tmux sessions when you exit the terminal. – Jesse_b Feb 3 '18 at 2:54
  • 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 killing tmux when you close a terminal. – B Layer Feb 3 '18 at 9:57
  • ...in fact a favorite feature of many users is the behavior I just described (which you want to subvert). For instance, you can log into a machine, start a tmux session, exit the session, logout then come back later and pick right back up where you left off. Not that you can't do whatever you want...just thought I'd mention it. – B Layer Feb 3 '18 at 10:05
0

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

A chapter on traps from a Bash scripting guide

A Unix & Linux Stack exchange question about traps

  • Would .bash_logout be called if the shell session is terminated trough closing the terminal window? – Kusalananda Jul 25 '18 at 18:44
  • most likely not - I edited my answer on how to approach this – Brett Holman Jul 26 '18 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.