List sessions
List tmux sessions with list-sessions -F for output a script can parse. With no server running the command prints an error and exits 1.
A script needs the names of the running tmux sessions, and the default listing has a timestamp and a status in it.
tmux list-sessions; echo "no server: $?"
tmux new-session -d -s one
tmux new-session -d -s two
tmux ls
echo ---
tmux ls -F '#{session_name}' | sortno server running on /tmp/tmux-0/default
no server: 1
one: 1 windows (created Fri Nov 6 14:07:08 2026)
two: 1 windows (created Fri Nov 6 14:07:08 2026)
---
one
twolist-sessions (alias ls) prints one line per session, and -F takes a format with #{...} variables, listed in the tmux manual. #{session_name} is all a script needs. The first command shows what happens before any session exists: tmux has no server process, says so on standard error and exits 1.
The default listing includes the creation time, which varies, so do not parse it. Treat exit status 1 as "none", but not necessarily as an error, when the server may be down. Running tmux ls inside the session it lists works the same.