我只想使用命令行列出正在运行的应用程序,如:Firefox、gedit、Nautilus 等。
注意:我不想列出所有正在运行的进程,只想列出正在运行的应用程序(比如手动启动的 GUI)。
答案1
wmctrl
和的组合xprop
提供了许多可能性。
示例 1:
running_gui_apps() {
# loop through all open windows (ids)
for win_id in $( wmctrl -l | cut -d' ' -f1 ); do
# test if window is a normal window
if $( xprop -id $win_id _NET_WM_WINDOW_TYPE | grep -q _NET_WM_WINDOW_TYPE_NORMAL ) ; then
echo "$( xprop -id $win_id WM_CLASS | cut -d" " -f4- )"", window id: $win_id"
fi
done
}
在这种情况下,输出可能看起来类似于以下内容:
"Firefox", window id: 0x032000a9
"Gnome-terminal", window id: 0x03a0000c
"Thunar", window id: 0x03600004
"Geany", window id: 0x03c00003
"Thunar", window id: 0x0360223e
"Mousepad", window id: 0x02c00003
"Mousepad", window id: 0x02c00248
"Xfce4-terminal", window id: 0x03e00004
示例 2:
running_gui_apps() {
applications=()
# loop through all open windows (ids)
for win_id in $( wmctrl -l | cut -d' ' -f1 ); do
# test if window is a normal window
if $( xprop -id $win_id _NET_WM_WINDOW_TYPE | grep -q _NET_WM_WINDOW_TYPE_NORMAL ) ; then
# filter application name and remove double-quote at beginning and end
appname=$( xprop -id $win_id WM_CLASS | cut -d" " -f4 )
appname=${appname#?}
appname=${appname%?}
# add to result list
applications+=( "$appname" )
fi
done
# sort result list and remove duplicates
readarray -t applications < <(printf '%s\0' "${applications[@]}" | sort -z | xargs -0n1 | uniq)
printf -- '%s\n' "${applications[@]}"
}
输出示例:
Firefox
Geany
Gnome-terminal
Mousepad
Thunar
Xfce4-terminal
您可以将该函数添加到您的脚本文件中~/.bashrc
或从脚本文件运行它。
答案2
wmctrl -l
可能是你想要的东西。首先安装它
sudo apt-get install wmctrl
您还可以将它与系统监视器的列表结合起来,默认情况下它显示“我的所有进程”,这意味着属于您作为用户的所有进程。
要仅获取应用程序名称,请运行:
编辑:
wmctrl -l|awk '{$3=""; $2=""; $1=""; print $0}'
答案3
介绍
xdotool
当你需要对窗口进行操作时,比如移动或调整大小,和的威力wmctrl
就会显现出来。然而,我坚信,仅仅为了清单正在运行的程序及其相关信息,xprop
以及qdbus
是两个足够的工具,除非用户想要这些工具来获得附加功能,否则安装xdotool
和wmctrl
是无意义的任务。在这个答案中,我想介绍两个脚本解决方案,分别是xprop
和qdbus
。
请注意,我绝不反对xdotool
或wmctrl
。我自己也广泛使用它们,但我发现与其他工具结合使用时它们功能更强大。以下是我使用它们的几个例子:
坐标系
下面的脚本仅使用 xprop 来提取活动窗口列表,仅过滤掉真正的窗口(不是像 Unity Launcher 或 Unity Panel 这样的 dock 类型)并显示它们的信息:
演示:
$ bash xprop_windows.sh
XID TYPE TITLE
--------------------------------
56623112| "x-terminal-emulator", "X-terminal-emulator"| "sakura"
81789126| "Navigator", "Firefox"| "Restore Session - Mozilla Firefox"
82002372| "Navigator", "Firefox"| "gui - How do I get a list of running applications by using the command line? - Ask Ubuntu - Mozilla Firefox"
33554444| "gnome-terminal", "Gnome-terminal"| "\"Terminal\""
33554486| "gnome-terminal", "Gnome-terminal"| "\"Terminal\""
脚本来源:
get_hex_xids()
{
xprop -root -notype _NET_CLIENT_LIST | \
awk 'BEGIN{printf "ibase=16"}\
{gsub(/\,/," ");for(i=1;i<=NF;i++) \
if ($i~/0x/) printf ";%s",substr(toupper($i),3) }'
}
convert_hex2dec()
{
HEXIDS=$(get_hex_xids)
echo $HEXIDS | bc
}
print_header()
{
printf "%s\t%s\t%s\n" "XID" "TYPE" "TITLE"
printf "%s\n" "--------------------------------"
}
list_info()
{
convert_hex2dec | while read line;
do
TYPE=$( xprop -id $line _NET_WM_WINDOW_TYPE | awk -F '=' '{print $2}' )
if [ $TYPE != "_NET_WM_WINDOW_TYPE_NORMAL" ]; then
continue
fi
CLASS=$(xprop -id $line WM_CLASS | awk -F '=' '{print $2}' )
NAME=$( xprop -id $line _NET_WM_NAME | awk -F '=' '{print $2}' )
printf "\n%s|%s|%s\n" "$line" "$CLASS" "$NAME"
done
}
print_header
list_info
总线
下面的代码基本上执行相同的任务,但是它首先过滤掉应用程序,然后列出其子窗口,最后提供有关它们的信息。
示例运行:
$ bash ~/bin/qdbus_windows.sh
Name: Terminal
Active :false
Children:
33554486|false|""Terminal""
33554444|false|""Terminal""
--------------
Name: Firefox Web Browser
Active :false
Children:
82002372|false|"gui - How do I get a list of running applications by using the command line? - Ask Ubuntu - Mozilla Firefox"
81789126|false|"Restore Session - Mozilla Firefox"
--------------
Name: MY CUSTOM TERMINAL
Active :true
Children:
56623112|true|"sakura"
--------------
代码本身:
#!/bin/bash
get_window_paths()
{
qdbus org.ayatana.bamf /org/ayatana/bamf/matcher org.ayatana.bamf.matcher.WindowPaths
}
get_running_apps()
{
qdbus org.ayatana.bamf /org/ayatana/bamf/matcher org.ayatana.bamf.matcher.RunningApplications
}
list_children()
{
qdbus org.ayatana.bamf "$1" org.ayatana.bamf.view.Children
}
window_info()
{
for window in "$@" ; do
XID=${window##*/}
TYPE=$(qdbus org.ayatana.bamf $window org.ayatana.bamf.window.WindowType)
NAME="$(qdbus org.ayatana.bamf $window org.ayatana.bamf.view.Name)"
ACTIVE=$(qdbus org.ayatana.bamf $window org.ayatana.bamf.view.IsActive)
MONITOR=$(qdbus org.ayatana.bamf $window org.ayatana.bamf.window.Monitor)
# printf "%s|%s|%s|%s\n" $TYPE $MONITOR $ACTIVE "$NAME"
printf "%s|%s|\"%s\"\n" $XID $ACTIVE "$NAME"
done
}
window_paths=( $( get_window_paths | tr '\n' ' ') )
apps_list=( $( get_running_apps | tr '\n' ' ' ) )
for app in ${apps_list[@]} ; do
#echo $app
printf "Name: "
qdbus org.ayatana.bamf $app org.ayatana.bamf.view.Name
printf "Active :"
qdbus org.ayatana.bamf $app org.ayatana.bamf.view.IsActive
printf "Children:\n"
# list_children $app
windows=( $( list_children $app | tr '\n' ' ' ) )
window_info "${windows[@]}"
printf "%s\n" "--------------"
done
一个稍微简单一点的命令,但需要过滤掉使用 Unity 的窗口堆栈 dbus 接口的输出。这本质上是一个功能我有.mkshrc
window_stack()
{
qdbus --literal com.canonical.Unity.WindowStack
/com/canonical/Unity/WindowStack \
com.canonical.Unity.WindowStack.GetWindowStack | \
awk -F '{' '{gsub(/\}|\]|,/,"");gsub(/\[/,"\n");print $2}' | \
awk '!/compiz/&&!/^$/ && $4!="\""$3"\"" { L[n++] = $0 }\
END { while(n--) print L[n] }'
}
示例运行:
$ window_stack
Argument: (usbu) 56623112 "x-terminal-emulator" true 0
Argument: (usbu) 82002372 "firefox" false 0
Argument: (usbu) 81789126 "firefox" false 0
Argument: (usbu) 33554486 "gnome-terminal" false 0
Argument: (usbu) 33554444 "gnome-terminal" false 0
qdbus 使用示例: