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

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

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

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

困难的问题,但我骗了它:)我在网上搜索了很多,我得到了一个解决方案。

最初,以下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打开,chrome,2个gnome-terminal windows和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。 所以,如果我是你,我会把每一个明显不应该被杀死的过程都掏出来,然后我会把其他一切都搞砸了!

你也可以再次'uniq',以免杀死重复的东西......

受到user55822给出的答案的启发,我制作了一个专门用于Xfce桌面环境的脚本,但它可以适用于使用与wmctrl正常交互的窗口管理器在任何桌面上使用。

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

 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“Desktop $ | xfce4-panel $”替换为该桌面上的任何工作。 脚本的这一部分正在做的是缩小wmctrl -l的结果, 使其不包含以“Desktop”或“xfce4-panel”结尾的任何内容。 因此,为了适应它,您只需运行wmctrl -l并查找要保持打开的窗口行末尾的内容。 至少在Xfce上,它最终将桌面本身列为一个窗口,这样如果没有grep命令,它最终会退出Xfce。