How can I get tmux to execute a command and then detach (That is, automatically in a single line without further input beyond initially starting the session)?

I don't know to write sophisticated scripts. But I used to do this in screen with a command like:

screen -S torrent -d -m qbittorrent-nox

But I have been reading that tmux is better and I would like to switch. But I couldn't figure out how to achieve what the above command does.

1 Answer 1


From the manual:

new-session [-AdDEPX] [-c start-directory] [-e environment] [-f flags] [-F format] [-n window-name] [-s session-name] [-t group-name] [-x width] [-y height] [shell-command]
(alias: new)
Create a new session with name session-name.
The new session is attached to the current terminal unless -d is given. window-name and shell-command are the name of and shell command to execute in the initial window.

You can replace the screen command with (naming the session for easy management as suggested by @cas):

tmux new-session -d -s torrent qbittorrent-nox 

It will create a detached session named torrent (rather than a number) with qbittorrent-nox running inside. When the command ends, so will the session.

You can then attach to this specific session (in case there are multiple created) with:

tmux attach-session -t torrent

Or if already having a tmux client attached to an other session, you can swith to this one instead with:

tmux switch-client -t torrent

(or simply navigate with ctrlb and s or (, )).

  • 1
    +1. Also, it's worthwhile specifying a session name with -s so you can easily attach to it (or add another window, or whatever, in that session) by name, rather than having to figure out what it is with tmux ls. e.g. tmux new-session -s qbt -d qbittorrent-nox. optionally name the window too with -n.
    – cas
    Jun 20, 2021 at 4:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .