Skip to content
shellhacks.dev
Tooling

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.

1 min read
Summarize in AI

A long running command should live in a session you can name, detach from and find again.

Shell
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: $?"
Text
session exists
build: 1 window
other: 1

new-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.

Share

More snippets

Tooling

Swap two panes in a window

Swap two panes in a window with swap-pane -s source -t target. The panes keep their IDs and running programs and trade positions, and prefix then braces do it.

2 min read
Tooling

Resize a pane

Resize a pane with resize-pane -x 30 for an exact width or -R 10 to move its edge. The keys are prefix then Ctrl and an arrow, one cell at a time.

1 min read
Tooling

Zoom a pane to full screen

Zoom one pane to fill the window with resize-pane -Z, or prefix then z, and call it again to restore. The window shows a Z flag while zoomed.

1 min read

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.