我想通过执行 shell 脚本来关闭所有打开的窗口(就像通过执行脚本打开多个应用程序一样),但我不知道如何。我怎样才能做到这一点?
您可能想要使用wmctrl -c. 例如,如果您尝试关闭 gedit,它会询问您是否要保存未保存的文件。
WIN_IDs=$(wmctrl -l | awk '$3 != "N/A" {print $1}')
for i in $WIN_IDs; do wmctrl -ic "$i"; done
Run Code Online (Sandbox Code Playgroud)
困难的问题,但我欺骗了它:)我在网上搜索了很多,并找到了解决方案。
下面的 bash 脚本首先读取所有打开的窗口的 ID,然后将每个 ID 转换为进程 PID。最后,它将所有 PID 转换为进程名称。它输出 PID 和进程名称。
这是脚本:
#!/bin/bash
#Script by the whole web. I wrote it but it's not mine
#creating a temp file
temp1=$(mktemp)
#Getting all the windows' IDs and writing them to a file (CREDITS TO http://stackoverflow.com/questions/2250757/is-there-a-linux-command-to-determine-the-window-ids-associated-with-a-given-pro)
xwininfo -root -children|sed -e 's/^ *//'|grep -E "^0x"|awk '{ print $1 }' > $temp1;
#reading every window ID and converting it to a PID & writing it to a file... (CREDITS TO http://www.linuxquestions.org/questions/programming-9/getting-the-pid-of-the-top-active-window-776938/)
temp2=$(mktemp)
while read id; do
xprop -id "$id" | awk '/_NET_WM_PID\(CARDINAL\)/{print $NF}' >> $temp2
done < $temp1
#removing temp1
rm -f $temp1
#another temp file
temp3=$(mktemp)
#removing duplicate entries from $temp2 file: (CREDITS TO http://www.unix.com/shell-programming-scripting/20364-remove-duplicate-lines-file.html)
uniq $temp2 > $temp3
#removing temp2
rm -f $temp2
#!!! Outputting the PIDs: !!!
echo "The following PIDs were found:"
cat $temp3
#!!! Optional: getting their process names: !!! (CREDITS TO http://info.w3calculator.com/free-code/linux/how-to-get-process-name-from-pid/)
echo "The above PIDs have the following names:"
while read pid; do
cat /proc/$pid/cmdline
#newline
echo
done < $temp3
#removing the last temp file...
rm -f $temp3
Run Code Online (Sandbox Code Playgroud)
这个脚本在我的机器上的输出,打开ettercap-gtk,chromium,2个gnome终端窗口和gedit,输出是:
The following PIDs were found:
9401
11194
1671
9401
10446
9401
10446
11194
10446
10434
9401
1653
1813
1671
1813
1454
1813
1653
1813
2340
2005
1996
1840
1809
1813
1809
1666
1781
1637
1773
1761
1653
1637
1653
1671
1669
1663
1653
1650
1649
1454
1400
1637
1653
1671
9401
The above PIDs have the following names:
/usr/lib/chromium-browser/chromium-browser
gedit/home/alex/Documents/macs
gnome-terminal-e/home/alex/Documents/WALLCH/start wait exec
/usr/lib/chromium-browser/chromium-browser
/usr/sbin/ettercap--gtk
/usr/lib/chromium-browser/chromium-browser
/usr/sbin/ettercap--gtk
gedit/home/alex/Documents/macs
/usr/sbin/ettercap--gtk
gksudo/usr/sbin/ettercap --gtk
/usr/lib/chromium-browser/chromium-browser
nautilus-n
/usr/lib/unity/unity-panel-service
gnome-terminal-e/home/alex/Documents/WALLCH/start wait exec
/usr/lib/unity/unity-panel-service
/usr/lib/gnome-settings-daemon/gnome-settings-daemon
/usr/lib/unity/unity-panel-service
nautilus-n
/usr/lib/unity/unity-panel-service
update-notifier
telepathy-indicator
/usr/lib/gnome-disk-utility/gdu-notification-daemon
/usr/lib/indicator-printers/indicator-printers-service
/usr/bin/gtk-window-decorator
/usr/lib/unity/unity-panel-service
/usr/bin/gtk-window-decorator
/home/alex/.dropbox-dist/dropbox
/usr/bin/gnome-screensaver--no-daemon
compiz
/usr/lib/bamf/bamfdaemon
/usr/lib/notify-osd/notify-osd
nautilus-n
compiz
nautilus-n
gnome-terminal-e/home/alex/Documents/WALLCH/start wait exec
bluetooth-applet
nm-applet
nautilus-n
/usr/lib/policykit-1-gnome/polkit-gnome-authentication-agent-1
/usr/lib/gnome-settings-daemon/gnome-fallback-mount-helper
/usr/lib/gnome-settings-daemon/gnome-settings-daemon
gnome-session--session=ubuntu
compiz
nautilus-n
gnome-terminal-e/home/alex/Documents/WALLCH/start wait exec
/usr/lib/chromium-browser/chromium-browser
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,不仅输出打开的窗口,还输出所有类似 GUI 的内容,例如 nm-applet。所以,如果我是你,我会 grep 出每个明显不应该被杀死的进程,然后我会杀死所有其他进程!
你也可以再次“uniq”,以免杀死重复的东西......