You are not logged in.
This script came about via this thread by Tunafish on #!: http://crunchbang.org/forums/viewtopic.php?id=13019
I still have it triggered from my "shutdown" script (you do have a shutdown script to clean up, don't you?) and it catches those times when you forget to close virtualbox or something and gives you a chance to save work. It goes round all the open windows and sends them a polite "close" message, letting them put up a "save stuff" dialogue if they want to.
It's pretty simple:
#!/bin/bash
# close_all_windows
# depends on wmctrl
IGNORE=' Conky($|[ \t])| tint2($|[ \t])| ADeskBar($|[ \t])| Desktop($|[ \t])| Exit\?($|[ \t])' # regular expression to check against window titles
ob_windows_list=($(wmctrl -l | awk '!/'"$IGNORE"'/{print $1}')) # get ids
for i in "${ob_windows_list[@]}"
do
wmctrl -i -R $i
wmctrl -i -c $i
while wmctrl -l | grep "$i" >/dev/null 2>&1
do
sleep 0.5
done
done
exit
...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), now on Bluesky, there's also some GitStuff )
Offline
^ This is great; frankly, I don't understand why nothing like this is implemented in most OSes.
Be excellent to each other, and...party on, dudes!
BunsenLabs Forum Rules
Tending and defending the Flame since 2009
Offline
Hey, how I missed this one?
when you forget to close virtualbox
which happens all the time...
Thanks, johnraff! Great stuff.
Offline
it isn't that simple actually.
i was looking for that script, so first off many thanks to johnraff for creating the "Useful HowTos" thread.
menawhile i'd started something myself and realised there's a few pitfalls:
firefox closes its window (and thus becomes non-existent to wmctrl) but continues to run for a while. fortunately wmctrl can also show a pid, so the script has to scan for that, too
i have at least one daemon running that is not affected by this at all, namely transmission-daemon. it needs to shut down gracefully, otherwise it can hang the shutdown process for quite a while
i will compare the two scripts (this one and my own half-baked attempt) and contribute some more.
Offline
Thanks for some good points there. I'll be eager to see your code - that script I wrote quite a long time ago.
To be honest, I've never noticed transmission holding things up, even when just shutting down directly from bl-exit.
(I'm hoping the how-to's index will be expanded as we remember more stuff to link to...)
...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), now on Bluesky, there's also some GitStuff )
Offline
To be honest, I've never noticed transmission holding things up, even when just shutting down directly from bl-exit.
sorry, i am using transmission-daemon without a gui (or with a web ui to be precise), so not the usual gtk interface.
as for the actual code, i am getting onto that right now.
testing requires logging in and out a few times, so i can't be on the forums at the same time.
Offline
Conky($|[ \t])| tint2($|[ \t])| ADeskBar($|[ \t])| Desktop($|[ \t])| Exit\?($|[ \t])
Those weeks of practicing regex are paying off, this doesn't turn my mind to mush! But couldn't the same be done by just searching for the processes in "ps -auxl" ?
"I have not failed, I have found 10,000 ways that will not work" -Edison
Offline
Conky($|[ \t])| tint2($|[ \t])| ADeskBar($|[ \t])| Desktop($|[ \t])| Exit\?($|[ \t])
Those weeks of practicing regex are paying off, this doesn't turn my mind to mush! But couldn't the same be done by just searching for the processes in "ps -auxl" ?
No, because we don't want PIDs, we're getting the window ID which is something the X system uses.
@ohnonot this is about closing windows. Are we going to have to go over all the running daemons and put them gently to sleep? I thought that was handled by the system shutdown command anyway?
...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), now on Bluesky, there's also some GitStuff )
Offline
(you do have a shutdown script to clean up, don't you?)
Alright, I'm gonna be the n00b that asks "how do you implement a shutdown script?"
Lenovo IdeaPad Yoga 13 | BunsenLabs Hydrogen (x64)
Intel Core i7-3537U | Intel HD4000 | 8GB DDR3 | 256GB SSD
Offline
^@Eraph
It's just a script that does whatever you want, before shutting down. bl-exit provides various shutdown options (run 'bl-exit-help' to see) and you can just use one of those at the end of your script. Something like:
#!/bin/bash
# shutdown.sh
# perform quick backup and cleanup before shutting down
~/scripts/quick_backup.sh
~/scripts/cleanup.sh
~/scripts/close_all_windows.sh
bl-exit --poweroff
Your quick_backup and cleanup scripts can be whatever you want, or omitted, or add whatever other actions you'd like to perform before shutting down. Call the script from an openbox menu item, or a launcher, or a keyboard combination, or...
...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), now on Bluesky, there's also some GitStuff )
Offline
Ah, I must admit I was expecting something that would automatically hook into system shut-down. Like if I were to Alt+X to bring up the shut-down panel and turn off my computer from there, this script would kick in automatically, regardless of how I initiated the shut-down. Now, surely such a thing must be possible, but I wonder how difficult it would be to implement
Lenovo IdeaPad Yoga 13 | BunsenLabs Hydrogen (x64)
Intel Core i7-3537U | Intel HD4000 | 8GB DDR3 | 256GB SSD
Offline
Ah, I must admit I was expecting something that would automatically hook into system shut-down. Like if I were to Alt+X to bring up the shut-down panel and turn off my computer from there, this script would kick in automatically, regardless of how I initiated the shut-down. Now, surely such a thing must be possible, but I wonder how difficult it would be to implement
You could add your commands/scripts to bl-exit; or run it from the keybind, and have it call bl-exit when it is done.
Be Excellent to Each Other...
The Bunsenlabs Lithium Desktop » Here
FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt «» BunsenLabs on DeviantArt
Offline
i suffered from temporary dementia when writing my own script.
it was overly complex; johnraff's works just fine.
fwiw, i was mostly concerned about firefox: it does keep a process running for a while, even though all windows are closed.
if i simply log out without closing firefox, i will be presented with a restored session (which i don't want), or even the "Well, this is embarassing..." dialog.
however, closing all windows is enough to avoid that, even though the firefox process is still running (and presumably not closed gracefully).
and my argument about transmission-daemon is true:
i have to send it a special command to exit, wait a few secs, then shut down the machine; otherwise the shutdown process tends to hang for up to 1min 30s.
here's my version of the script:
i just added one little thing to avoid pointless flickering through all desktops with open windows.
oh, and i'd really like to understand the ignore regex better (i am awk-illiterate); if i have a document called "Conky" opened for editing in geany, wouldn't that be ignored, too?
#!/bin/bash
# close_all_windows (johnraff)
# depends on wmctrl
IGNORE=' Conky($|[ \t])| tint2($|[ \t])' # regular expression to check against window titles
ob_windows_list=($(wmctrl -l | awk '!/'"$IGNORE"'/{print $1}')) # get ids
for i in "${ob_windows_list[@]}"
do
wmctrl -i -c $i
sleep 0.2
wmctrl -l | grep "$i" >/dev/null 2>&1 && wmctrl -i -R $i
while wmctrl -l | grep "$i" >/dev/null 2>&1
do
sleep 0.2
done
done
if pidof transmission-daemon >/dev/null 2>&1
then
transmission-remote --exit
echo -n "Waiting for transmission-daemon to shut down"
while pidof transmission-daemon >/dev/null 2>&1; do
sleep 0.1
echo -n "."
done
fi
exit
Offline
Eraph wrote:Ah, I must admit I was expecting something that would automatically hook into system shut-down. Like if I were to Alt+X to bring up the shut-down panel and turn off my computer from there, this script would kick in automatically, regardless of how I initiated the shut-down. Now, surely such a thing must be possible, but I wonder how difficult it would be to implement
You could add your commands/scripts to bl-exit; or run it from the keybind, and have it call bl-exit when it is done.
What I mean is, I like having the dialog pop up, choosing to log out, suspend, hibernate or shut down or whatever. If there was a way to call this script before the system shut-down process started, regardless of how I called shut-down, that would be ideal. I'll have a look and see if I can find anything about system event hooks or anything.
--EDIT
That didn't take long.
Last edited by Eraph (2016-06-02 22:28:07)
Lenovo IdeaPad Yoga 13 | BunsenLabs Hydrogen (x64)
Intel Core i7-3537U | Intel HD4000 | 8GB DDR3 | 256GB SSD
Offline
...
What I mean is, I like having the dialog pop up, choosing to log out, suspend, hibernate or shut down or whatever. If there was a way to call this script before the system shut-down process started, regardless of how I called shut-down, that would be ideal. I'll have a look and see if I can find anything about system event hooks or anything.
As I said
You could add your commands/scripts to bl-exit...
Or do you mean having the script run even if you bypass bl-exit completely, say by using terminal commands?
Be Excellent to Each Other...
The Bunsenlabs Lithium Desktop » Here
FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt «» BunsenLabs on DeviantArt
Offline
Maybe I misunderstood, does bl-exit fire on shut-down/restart?
Lenovo IdeaPad Yoga 13 | BunsenLabs Hydrogen (x64)
Intel Core i7-3537U | Intel HD4000 | 8GB DDR3 | 256GB SSD
Offline
Maybe I misunderstood, does bl-exit fire on shut-down/restart?
'/usr/bin/bl-exit' is what runs the shutdown gui dialog, which I guess you are referring to as the "popup". It is a python script, so you could add 'call("/path/to/script")' lines in the appropriate places.
Be Excellent to Each Other...
The Bunsenlabs Lithium Desktop » Here
FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt «» BunsenLabs on DeviantArt
Offline
I get you now. Let's say you have shut-down mapped to a physical button though, it wouldn't call the script in that case. I edited my previous post to include this possible solution, I might give it a try later.
Lenovo IdeaPad Yoga 13 | BunsenLabs Hydrogen (x64)
Intel Core i7-3537U | Intel HD4000 | 8GB DDR3 | 256GB SSD
Offline
I get you now. Let's say you have shut-down mapped to a physical button though, it wouldn't call the script in that case. I edited my previous post to include this possible solution, I might give it a try later.
What is the command for the keybind?
Could it be something like
/path/to script && <shutdown-command>
Or, if the script was called from bl-exit, the shutdown keybind can just be
bl-exit -p
For usage, see
bl-exit --help
Last edited by damo (2016-06-02 23:12:54)
Be Excellent to Each Other...
The Bunsenlabs Lithium Desktop » Here
FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt «» BunsenLabs on DeviantArt
Offline
Nah, I'm not talking about keybinds. I'm talking about, when the system gets the shut-down command from anything, be it from bl-exit, from a command in Terminal, or from pressing the power switch on your machine. So no matter how the shut-down is initiated, this script could be run. The solution seems to be to put it in/symlink from these directories:
/etc/rc0.d/S00shutdownscriptfile # Executes on shut-down
/etc/rc6.d/S00shutdownscriptfile # Executes on reboot
Lenovo IdeaPad Yoga 13 | BunsenLabs Hydrogen (x64)
Intel Core i7-3537U | Intel HD4000 | 8GB DDR3 | 256GB SSD
Offline