I want to close all opening windows by executing a shell script (just like open multiple applications by executing a script) but I don't know how. How can I achieve this?
I want to close all opening windows by executing a shell script (just like open multiple applications by executing a script) but I don't know how. How can I achieve this?
You might want to use
wmctrl -c
. If you're trying to close gedit for example, it will ask you if you want to save unsaved files.Difficult problem, but I tricked it :) I searched a lot in the web and I resulted to a solution.
The following bash script, initially, reads all the open windows' IDs, then, it converts every of the IDs to process PIDs. Finally, it converts all the PIDs to process-names. It outputs both the PIDs and the process-names.
Here is the script:
The output of this script on my machine, with ettercap-gtk open, chromium, 2 gnome-terminal windows and gedit, the output is:
As you can see, not only the open windows are being outputed, but also everything GUI-like, like nm-applet. So, if I were you, I would grep out every process that is obvious it shouldn't be killed, and then I would killall everything else!
You can also again 'uniq' so as to not kill duplicate things...
Inspired by the answer given by user55822, I have made a script specifically to be used in the Xfce Desktop Environment, but it could be adapted to be used on any desktop using a window manager that interacts properly with wmctrl.
My script takes the extra step to wait until all the windows are actually closed so that if called from another script it won't return too soon. Here is my script for closing all the open windows other than panels and the Desktop itself:
To adapt it for a desktop other than Xfce, you would need to replace grep -vwE "Desktop$|xfce4-panel$" with whatever works on that desktop. What that part of the script is doing is narrowing down the results of wmctrl -l to not include anything ending in the word "Desktop" or "xfce4-panel". So to adapt it, you would just run wmctrl -l and look for what's at the end of the lines for windows you want to stay open. On Xfce at least, it ends up listing the Desktop itself as a window so that without the grep command, it ends up logging out of Xfce.