tmux

How to create new tmux session if none exists


I am trying to figure out how to attach to a tmux session if a named tmux session exists, if not I want to create a new one with the given name.

Currently, I know of a few tmux commands which can partly achieve what I am looking for, but its not clear how to combine them together to get what I am looking for:

Thus, I would like to create a tmux script, so that this happens automatically, instead of having to manually create it everytime I need to log into a sessions.

How can I write a automatic script so as to create a new tmux session (if a given session name doesnt exist) or attach to a session name (if it exists)?


Solution

  • Alternately, you can add

    new-session
    

    to your .tmux.conf - that will create a default session on server start.

    Then tmux attach will either attach to the current session (running server, that is), or create a new session (start the server, read the config file, issue the new-session command) and attach to that.