Attach or create in one command
Attach to a tmux session if it exists and create it otherwise with tmux new-session -A -s name. In a script without a terminal use has-session instead.
A shell alias should give you your session whether or not one is already running.
tmux new-session -d -s main
tmux new-session -d -s main; echo "plain, second time: $?"
tmux new-session -A -s main; echo "with -A and no terminal: $?"
tmux has-session -t main 2>/dev/null || tmux new-session -d -s main
tmux ls -F '#{session_name}'duplicate session: main
plain, second time: 1
open terminal failed: not a terminal
with -A and no terminal: 1
main-A makes new-session behave like attach-session when the named session exists, and like a normal create when it does not, as the tmux manual says. The second command shows what a plain create does with a name that is taken: it fails with "duplicate session". The third run has no terminal, so the attach half of -A fails. In a real terminal that same command is the whole alias. The has-session line is the form for a script, and the listing shows one session.
-A needs a terminal to attach to, so use it in a shell alias and never in cron or CI. Adding -d to it detaches the other clients that are attached, which can surprise a colleague sharing the session. A command given after the session name is ignored when the session already exists.