如何使用脚本关闭所有打开的窗口?

如何使用脚本关闭所有打开的窗口?

我想通过执行 shell 脚本来关闭所有打开的窗口(就像通过执行脚本打开多个应用程序一样),但我不知道怎么做。我该如何实现?

答案1

您可能想要使用wmctrl -c。例如,如果您尝试关闭 gedit,它会询问您是否要保存未保存的文件。

WIN_IDs=$(wmctrl -l | awk '$3 != "N/A" {print $1}')
for i in $WIN_IDs; do wmctrl -ic "$i"; done

答案2

问题很难,但是我解决了:)我在网上搜索了很多,最终找到了解决方案。

以下 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

在我的计算机上,该脚本的输出为:ettercap-gtk 打开,chromium,2 个 gnome-terminal 窗口和 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

如您所见,不仅输出了打开的窗口,还输出了所有类似 GUI 的内容,如 nm-applet。因此,如果我是您,我会 grep 出每个显然不应被终止的进程,然后我会终止所有其他进程!

您还可以再次使用“uniq”以避免杀死重复的东西......

答案3

受到 user55822 给出的答案的启发,我制作了一个专门用于 Xfce 桌面环境的脚本,但它可以适用于任何桌面,只要有一个窗口管理器可以与之正确交互控制端

我的脚本采取了额外的步骤来等待所有窗口都实际关闭,这样如果从另一个脚本调用它,它就不会太快返回。这是我的脚本,用于关闭除面板和桌面本身之外的所有打开的窗口:

WIN_IDs=$(wmctrl -l | grep -vwE "Desktop$|xfce4-panel$" | cut -f1 -d' ')
for i in $WIN_IDs; do wmctrl -ic "$i"; done

# Keep checking and waiting until all windows are closed 
while [ $WIN_IDs ]; do 
    sleep 0.1; 
    WIN_IDs=$(wmctrl -l | grep -vwE "Desktop$|xfce4-panel$" | cut -f1 -d' ')
done

要使其适用于 Xfce 以外的桌面,您需要替换grep -vwE "桌面$|xfce4-panel$"在该桌面上运行的任何程序。该脚本的这一部分正在缩小结果范围wmctrl-l不包括以单词“Desktop”或“xfce4-panel”结尾的任何内容。因此,要调整它,您只需运行wmctrl-l并查找您想要保持打开的窗口行尾的内容。至少在 Xfce 上,它最终会将桌面本身列为一个窗口,因此如果没有 grep 命令,它最终会退出 Xfce。

相关内容