LewlSauce
LewlSauce

Reputation: 5872

How to target a particular tmux session

I have a script that uses Tmux to run commands on a remote system and sometimes it screws up due to the way Tmux's list-windows command is run. For example, if I have two Tmux sessions on the remote server: session1 and session2. Depending on whether or not I am active in session 1 or session 2, running tmux list-windows from outside of Tmux gives me two completely different results:

Here's an example:

[root:kali:]# tmux ls
session1: 2 windows (created Tue Nov  5 18:22:58 2019)
session2: 3 windows (created Wed Nov  6 18:21:04 2019)

[root:kali:]# tmux list-windows
0: zsh* (1 panes) [215x53] [layout 5de0,215x53,0,0,30] @28 (active)
1: tool (1 panes) [80x24] [layout 5963,80x24,0,0,33] @31
2: script- (1 panes) [215x53] [layout 5de4,215x53,0,0,34] @32

[root:kali:]# tmux a -t session1
[detached (from session session1)]

[root:kali:]# tmux list-windows
0: zsh- (1 panes) [215x53] [layout bb5d,215x53,0,0,0] @0
1: msf* (1 panes) [215x53] [layout dde6,215x53,0,0,27] @25 (active)

In the example above, all I did was simply attach to session 1 and then detach from it. Next, when running tmux list-windows, you can clearly see that it shows the windows that are from session1 instead of session2.

Is there a way with Tmux to simply list the windows from a particular session from outside of Tmux? This would literally solve all of my issues that I've been facing with this for quite some time.

Upvotes: 3

Views: 2920

Answers (1)

jeremysprofile
jeremysprofile

Reputation: 11425

Yes, you can specify a particular session for most tmux commands.

tmux list-windows -t session2

-t is how you choose to attach to a certain session (tmux a -t session1), or even a certain session/window/pane combination.

For instance, if I have a session sess, that has 3 windows, each with 4 panes, and I want to run htop in the first window, third pane, I'd do

tmux send-keys -t sess:0.2 'htop' Enter

as window/panes in tmux are 0-indexed.

Upvotes: 6

Related Questions