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
if ! tmux has-session -t "$SESH" 2>/dev/null; then exit; fishould ensure zsh exits along with the terminal that hosts it when there are no session named$SESHleft. What's the output oftypeset -p TMUX SESH; tmux has-session -t "$SESH"; echo $?if run in that shell that should have exited but didn't?typeset -p TMUX SESH; tmux has-session -t "$SESH"; echo $?istypeset: no such variable: TMUX typeset SESH=wezterm-gui no server running on /tmp/tmux-1000/default 1on the shell that should have been exited. However. executinglist-sessionsgivesno server running on /tmp/tmux-1000/defaulttmux list-sessionscall in the .zshrc before theif ! .... Could be thetmux attach-sessionreturns in your case before the session is not fully torn down yet. Maybe adding asleep 0.2after thetmux attach-session -t "$SESH"would help.