For a while, I used screen -dURS to solve this problem.

The goal is to be able to run a command that attaches to an existing session with a specific name, or creating a session with a specific name and running a command in that new session. A specific example would be to load a server or daemon if it's not running, or to simply attach to the session if it is running.

I have been reading man tmux and the closest I have gotten to this goal is the following:

tmux new-session -AD -t TMUXSessionName 'echo Banana'

The -A flag makes new-session behave like attach-session if session-name already exists; in the case, -D behaves like -d to attach-session.

If -t is given, the new session is grouped with target-session. This means they share the same set of windows - all windows from target-session are linked to the new session and any subsequent new windows or windows being closed are applied to both sessions. The current and previous window and any session options remain independent and either session may be killed without affecting the other. Giving -n or shell-command are invalid if -t is used.

Am I misunderstanding these flags? Am I trying to do something that's not supported by tmux natively?

Note: I find tmux easier to explain to less technically savy people and because of the way it allows sessions to be shared easily, I'd like to be able to switch to tmux from screen and this issue is the only thing I haven't been able to figure out to allow me to do the switch completely.


The trick was to make the following change:

tmux new-session -AD -s TMUXSessionName 'echo Banana'

So that we are using -s and not -t.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.