Start a named tmux session
Start a tmux session in the background with a name using tmux new-session -d -s name. A script can then send it commands without ever attaching.
A long running command should live in a session you can name, detach from and find again.
tmux new-session -d -s build 'sleep 300'
tmux has-session -t build && echo "session exists"
tmux list-sessions -F '#{session_name}: #{session_windows} window'
tmux has-session -t other 2>/dev/null; echo "other: $?"session exists
build: 1 window
other: 1new-session -d creates the session without attaching to it, -s names it, and a trailing argument is the command to run in the first window. The options are in the tmux manual. has-session exits 0 when the session exists and 1 when it does not, which makes it the test for a script that should start a session only once.
Without a terminal, new-session without -d fails with "open terminal failed", so a script run from cron or CI must always detach. A session that runs a command ends when the command exits, unless you start a shell and send the command with send-keys. The output is from tmux 3.3a.