20

Is there a way how to detect what process runs in the active pane?

tmux: Find "my" window, check if it's active is related, however, it deals with the detection from within the pane.

I need to detect this from an external command (not from within the tmux session).

The problem most likely boils down to two steps:

  1. Detect which pane is active
  2. Detect what process runs in the given pane
1

3 Answers 3

22

Turns out tmux has powerful formatting capabilities for the list-panes command:

$ tmux list-panes -F '#{pane_active} #{pane_pid}'
0 4993
0 5382
1 6189

The command above will print

1 <process_pid>

for the active pane.

Fromatting options are described in man tmux:

 pane_active                     1 if active pane
 pane_pid                        PID of first process in pane

For the discussion of the term "first process in pane", see What does the "first pid of a pane" in tmux mean?

1
  • Use tmux list-panes -F '#{pane_active} #{pane_index} #{pane_pid}' to also show the index of the pane, which you can get by pressing <prefix>b
    – Kay
    Aug 2, 2022 at 17:51
0

Just for reference, the #2 problem can be resolved by

ps fut

It prints out all processes running in current tty (each pane should have unique tty)

0

If what you're after is the name of the current process, try:

$ tmux display -p "${pane_current_command}"

However, that's not always correct, e.g. when the process was started from a shell script or by pressing v in less to invoke $EDITOR. In those cases, this works better

$ tmux run "ps -o comm= -t '#{pane_tty}'" | tail -1

Of course you can grab additional details by changing the arguments to ps. Or you can replace #{pane_tty} with another tty.

You must log in to answer this question.

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