0

I have the code below in my ~/.zshrc to create or attach to a tmux session named after the current terminal emulator. It works as intended in most cases, but I'm encountering an issue when I attach to a detached session.

if [ -z "$TMUX" ]; then
  SESH=$(ps -p $PPID -o comm=)
  if ! tmux has-session -t "$SESH" 2>/dev/null; then
    tmux new-session -s "$SESH"
  else
    tmux attach-session -t "$SESH"
  fi
  if ! tmux has-session -t "$SESH" 2>/dev/null; then
    exit
  fi
fi

When I run kill-session or manually close all windows, the terminal emulator exits.

When I detach from the session, terminal emulator remains open.

However, When I attach to a detached session and then run kill-session or manually close all windows, the terminal emulator does not exit; only the tmux session is exited.

How can I modify that code to ensure that the terminal emulator exits when I kill the tmux session after attaching to it?

Here is my .tmux.conf for reference, but I don't think that would be relevant to this behaviour.

Executing typeset -p TMUX SESH; tmux has-session -t "$SESH"; echo $? on the shell that should have been exited gives

typeset: no such variable: TMUX typeset 
SESH=wezterm-gui no server running on /tmp/tmux-1000/default 
1

while executing list-sessions outputs

no server running on /tmp/tmux-1000/default
4
  • That script is just a part of my .zshrc file. source. My .tmux.conf but I don't think that would be relevant to this behaviour Commented Nov 18, 2024 at 7:33
  • I can't reproduce here. That if ! tmux has-session -t "$SESH" 2>/dev/null; then exit; fi should ensure zsh exits along with the terminal that hosts it when there are no session named $SESH left. What's the output of typeset -p TMUX SESH; tmux has-session -t "$SESH"; echo $? if run in that shell that should have exited but didn't? Commented Nov 18, 2024 at 8:03
  • Output of typeset -p TMUX SESH; tmux has-session -t "$SESH"; echo $? is typeset: no such variable: TMUX typeset SESH=wezterm-gui no server running on /tmp/tmux-1000/default 1 on the shell that should have been exited. However. executing list-sessions gives no server running on /tmp/tmux-1000/default Commented Nov 18, 2024 at 9:04
  • You could add a tmux list-sessions call in the .zshrc before the if ! .... Could be the tmux attach-session returns in your case before the session is not fully torn down yet. Maybe adding a sleep 0.2 after the tmux attach-session -t "$SESH" would help. Commented Nov 18, 2024 at 12:16

1 Answer 1

0

Better approaches might be:

Either a) exec tmux, fully replacing the shell process with the tmux process; once tmux exits, there is nothing more to return to, so the terminal will close.

Or b) add the -P option to your tmux detach-client keybind, so that it sends a SIGHUP to the parent shell. (No equivalent for kill-session.)

1
  • I want to keep the terminal when the session is detached, only exit when it killed. Using approach a) closes the terminal when the session is detached. Commented Nov 18, 2024 at 12:53

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.