如何检查 gnome-terminal 中运行的程序

发布于 2024-08-19 04:24:00 字数 455 浏览 8 评论 0原文

我想编写一个程序,打印当前聚焦的窗口名称,如果它是 gnome 终端,则打印当前 gnome 终端选项卡内正在运行的程序(例如 vim,如果 vim 会话正在运行)。

为了获取当前聚焦的窗口名称,我使用了:

<块引用>

xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)"

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

如果当前窗口是 gnome-terminal,这将返回 'gnome-terminal'。

但是我怎样才能找到 gnome-terminal 内运行的程序(更准确地说:在当前 gnome-terminal 选项卡内)?我想过使用 dbus 但 gnome-terminal 似乎不支持它。

I want to write a program which prints the current focused window name and if it is a gnome-terminal, then 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长亭外,古道边 2024-08-26 04:24:00

我需要解决同样的问题,经过一番调查后我发现
wmctrl 和 pstree 以相同的顺序打印进程。

免责声明:我不确定情况总是如此,但在我的情况下,我使用此方法打开“备忘单”进行手动审查,会立即检测到问题,因此没有问题。

这是一个演示脚本,运行时将输出与当前活动终端窗口相对应的 pstree 的正确行。为了调试,它将中间步骤打印到 ~/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

预期输出:

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

然后挑选所需的子项。

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.

水水月牙 2024-08-26 04:24:00

获取 gnome 终端 PID,并检查哪些进程将此数字作为 PPID。

我已经回答了几天前非常相似的问题,请参阅此链接了解详细信息。

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.

初熏 2024-08-26 04:24:00

谢谢亚当!我快到了。使用 xprop 我可以获得 gnome 终端的 PID (6736)。但不幸的是,所有 gnome 终端窗口和选项卡都只有一个进程。使用两个打开的 gnome-terminal 窗口查看此 pstree 输出:

 -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 current opening gnome-terminal tab?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文