3

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.

3
  • 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
    Commented Feb 3, 2018 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
    Commented Feb 3, 2018 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
    Commented Feb 3, 2018 at 10:05

1 Answer 1

4

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .