tmux: Detect which process runs in the active pane

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

Solution 1:

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?