Skip to content
shellhacks.dev
Command Line

Run a Command in a New Detached tmux Session

Start a command in the background with tmux new -d -s name 'command'. The session ends when the command does, unless remain-on-exit is set.

1 min read
Summarize in AI

A long job should keep running after you log out, and you want to find it again by name.

Shell
tmux new -d -s job 'echo hi; sleep 1'
tmux ls
sleep 2
tmux ls
Text
job: 1 windows (created Fri Nov  6 18:40:28 2026)
no server running on /tmp/tmux-0/default

With -d the session starts without attaching, and the shell command after the options runs in its first window. The session lives only as long as that command, so the first tmux ls lists it and the second, after the command has finished, reports that no server is running. This is the documented behavior of new-session. To keep the window and its output after the command ends, run tmux set-option -g remain-on-exit on first.

Output from a job that ended is gone unless the window is kept or the output is also written to a file. For long jobs that must survive reboots, use systemd or cron rather than tmux. Give each session a distinct name, since a second new -s job fails with a "duplicate session" error, and tmux new -A -s job attaches instead.

Share

More snippets

Command Line

Avoid Parsing ls Output in Shell Scripts

Do not loop over the output of ls in a script. File names with spaces or a newline split into pieces, while a glob such as ./* hands over each name whole.

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.