Tmux attach to existing sessions or create new ones

tmux

I am looking for a ssh / tmux solution that would act like this:

  • if there is no session, create one
  • if there is a session and nobody is connected to it, create another one

Mainly I want to be able to create new sessions to the same server, obviously if there is more than one session that has nobody connected to it, it should pick the first one.

This should enable me to put this as default command for ssh connections.

My current solution ssh -t 'tmux a || tmux || /bin/bash' doesn't work as expected because when you try to connect again it will connect to the existing session, and in this case I want a new one.

Best Answer

I'm not sure since what versione but now you can use

tmux new -A -s <session-name>

The -A flag makes new-session behave like attach-session if session-name already exists