1

In my .zshrc, I have a bit of logic that will automatically start a tmux session or, if sessions are already running, prompt to attach to one of them or create a new one and attach to that. This all works great, but I have a new requirement that I can't seem to get right: When a new session starts and a particular process isn't running, then start that process and create a new window; if said process is running somewhere, then just start a new session as normal.

In this case, I want irssi to start automatically in one session. The following (where $choice is per the selection process as outlined above) is as close as I've come to getting it to work:

exec tmux -2 new-session -A -s $choice $(pgrep irssi || echo "irssi; new-window")

This works perfectly when I have only one session -- which is most of the time -- but if I try to create a new session, then it just immediately detaches.

If I factor out the above into a conditional:

if pgrep irssi; then
  exec tmux -2 new-session -A -s $choice
else
  exec tmux -2 new-session -A -s $choice "irssi; new-window"
  # splitting out `tmux new-window` also doesn't work
fi

...I have the opposite problem in that now the secondary (non-irssi) sessions start fine, without detaching immediately, but the irssi-session only gets an irssi window and the new shell window doesn't appear. (This is arguably closer to what I want than the first version, but because I almost always only have one session, that wins as this version forces me to do Prefix-c for a new window.)

Your Answer

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

Browse other questions tagged or ask your own question.