开发者

How to check which program runs inside gnome-terminal

I want to write a program which prints the current focused window name and if it is a gnome-terminal, 开发者_如何学Cthen prints out the running program inside the current gnome-terminal tab (for example vim, if a vim session is running).

To get the currently focused window name, I used:

xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)"

xprop -id 0x220ad5a | grep "WM_CLASS(STRING)"

If the current window is a gnome-terminal, this will return 'gnome-terminal'.

But how can I find out the program running inside gnome-terminal (more precisely: inside the current gnome-terminal tab)? I thought about using dbus but gnome-terminal does not seem to support it.


I needed to solve the same problem and after some investigation I discovered that wmctrl and pstree prints processes in the same order.

DISCLAIMER: I'm not sure this is always the case but in my case where I use this method to open up a "cheatsheet" for manual review a problem with it would be detected immediately and so for had no problem.

Here is a demo-script that when run will output the correct row of the pstree that corresponds to the currently active terminal window. For debugging it prints intermediate steps into ~/debug.txt

#!/bin/bash
winid=$(xprop -root | awk '/_NET_ACTIVE_WINDOW\(WINDOW)/ {print $NF}' | xargs printf "%#010x\n")
echo 'winid:'$winid >> ~/debug.txt
winclass=$(xprop -id $winid | awk '/WM_CLASS/ {print $NF}')
niceclass=${winclass//\"/}
echo 'winclass:'$niceclass >> ~/debug.txt
if [ $niceclass == "Gnome-terminal" ]
then
    terminalPID=$(xprop -id $winid | awk '/_NET_WM_PID/ {print $NF}')
    echo 'winPID:'$terminalPID >> ~/debug.txt
     # get inx of window for this PID
    termInx=$(wmctrl -l -p | grep $terminalPID | awk '/'"$winid"'/ {print NR}')
    echo 'term inx:'$termInx >> ~/debug.txt
     # Take the childprocess of that inx and PID
    shell_process=$(pstree -p $terminalPID | sed "s/.*(1998)//" | sed "s/\W*//" | awk  'NR=='$termInx)
    pstree -p $terminalPID >> ~/debug.txt
    echo 'found process:'$shell_process >> ~/debug.txt
    echo 'found process:'$shell_process
fi

Expected output:

tony@tony-mini:~$ ./test_so.sh 
found process:bash(8001)---test_so.sh(9869)---test_so.sh(9885)-+-awk(9889)

Then pick out the desired child.


Get the gnome terminal PID, and check which processes have this number as PPID.

I have answered a very similar question few days ago, see this link for details.


Thanks Adam! I am almost there. With xprop I can get the PID of the gnome-terminal (6736). But unfortunately, there is only one process for all gnome-terminal windows and tabs. See this pstree output with two opened gnome-terminal windows:

 -gnome-terminal(6736)-+-bash(6738)---vim(6780) 

  |                    |-bash(7026)---pstree(7045) 

  |                    | `-{gnome-terminal}(6740) 

Is there a way to find out the bash pid of the currently opened gnome-terminal tab?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜