'tmux - attach to a session and specify window

I have a script (.sh) and I want it to run in a existing tmux session. I have 1 session with 8 windows in.

Is there a command like tmux a -t session-name, which also specify the window?

And would a script like this work?

#!/bin/bash tmux a -t session-name #What ever to write to specify window# java -jar -Xmx4G -Xms4G Spigot.jar



Solution 1:[1]

You can change the active window of a session before you attach to the session.

tmux -t <session-name> select-window -t <windowID>
tmux a -t <session-name>

You can combine two tmux commands as well.

tmux -t session-name select-window -t <windowID> \; a

If you really want to run java, presumably you want to create a new window with new-window, rather than select an existing one with select-window.


Newer versions of tmux (at least 1.9; did the above ever work, perhaps in 1.6?) no longer appear to have a -t option to specify the session to apply commands to. Instead, each individual command specifies the session.

tmux select-window -t <session-name>:<windowID> \; a -t <session-name>

Solution 2:[2]

For tmux version 2.1 this works

tmux a -t  <session-name> \; select-window -t <windowID> \;

Solution 3:[3]

You can specify the window after the session separated by a colon.

tmux a -t session:window 

You can even attach to a specific pane.

tmux a -t session:window.pane

Pane can be a number starting from 0. Window can be a number or name. man tmux has more info about different syntaxes allowed for target-session, target-window, and target-pane.

target-window (or src-window or dst-window) specifies a window in the form session:window...

This syntax works on any other command like send-keys. If it's not working you may be on an older version of tmux and need to upgrade or try an approach suggested in the other answers.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 dowewas
Solution 3