You are not logged in.

#1 2015-11-02 08:29:07

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 5,740
Website

"Close open windows" script for shutdown.

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

John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
In case you forget, the rules.

Online

#2 2015-11-02 08:35:10

pvsage
Internal Affairs
Registered: 2015-09-29
Posts: 1,433

Re: "Close open windows" script for shutdown.

^ 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

#3 2015-12-07 11:18:03

Snap
Member
Registered: 2015-10-02
Posts: 465

Re: "Close open windows" script for shutdown.

Hey, how I missed this one?

when you forget to close virtualbox

which happens all the time...

Thanks, johnraff! Great stuff.

Offline

#4 2016-06-01 06:26:23

ohnonot
...again
Registered: 2015-09-29
Posts: 3,917
Website

Re: "Close open windows" script for shutdown.

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:

  1. 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

  2. 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

#5 2016-06-01 08:26:34

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 5,740
Website

Re: "Close open windows" script for shutdown.

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...)


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
In case you forget, the rules.

Online

#6 2016-06-01 19:19:57

ohnonot
...again
Registered: 2015-09-29
Posts: 3,917
Website

Re: "Close open windows" script for shutdown.

johnraff wrote:

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

#7 2016-06-01 21:09:34

Horizon_Brave
Operating System: Linux-Nettrix
Registered: 2015-10-18
Posts: 1,473

Re: "Close open windows" script for shutdown.

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

#8 2016-06-02 02:35:56

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 5,740
Website

Re: "Close open windows" script for shutdown.

Horizon_Brave wrote:

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?


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
In case you forget, the rules.

Online

#9 2016-06-02 05:26:02

Eraph
Member
From: /au/qld/bne
Registered: 2016-02-29
Posts: 282
Website

Re: "Close open windows" script for shutdown.

johnraff wrote:

(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

#10 2016-06-02 06:31:54

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 5,740
Website

Re: "Close open windows" script for shutdown.

^@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...


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
In case you forget, the rules.

Online

#11 2016-06-02 06:36:16

Eraph
Member
From: /au/qld/bne
Registered: 2016-02-29
Posts: 282
Website

Re: "Close open windows" script for shutdown.

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 big_smile


Lenovo IdeaPad Yoga 13 | BunsenLabs Hydrogen (x64)
Intel Core i7-3537U | Intel HD4000 | 8GB DDR3 | 256GB SSD

Offline

#12 2016-06-02 15:53:54

damo
....moderator....
Registered: 2015-08-20
Posts: 5,059

Re: "Close open windows" script for shutdown.

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 big_smile

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...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#13 2016-06-02 19:42:26

ohnonot
...again
Registered: 2015-09-29
Posts: 3,917
Website

Re: "Close open windows" script for shutdown.

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

#14 2016-06-02 22:24:09

Eraph
Member
From: /au/qld/bne
Registered: 2016-02-29
Posts: 282
Website

Re: "Close open windows" script for shutdown.

damo wrote:
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 big_smile

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

#15 2016-06-02 22:29:01

damo
....moderator....
Registered: 2015-08-20
Posts: 5,059

Re: "Close open windows" script for shutdown.

Eraph wrote:

...
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

damo wrote:

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...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#16 2016-06-02 22:34:52

Eraph
Member
From: /au/qld/bne
Registered: 2016-02-29
Posts: 282
Website

Re: "Close open windows" script for shutdown.

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

#17 2016-06-02 22:53:45

damo
....moderator....
Registered: 2015-08-20
Posts: 5,059

Re: "Close open windows" script for shutdown.

Eraph wrote:

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...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#18 2016-06-02 22:56:21

Eraph
Member
From: /au/qld/bne
Registered: 2016-02-29
Posts: 282
Website

Re: "Close open windows" script for shutdown.

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

#19 2016-06-02 23:10:15

damo
....moderator....
Registered: 2015-08-20
Posts: 5,059

Re: "Close open windows" script for shutdown.

Eraph wrote:

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...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#20 2016-06-02 23:16:30

Eraph
Member
From: /au/qld/bne
Registered: 2016-02-29
Posts: 282
Website

Re: "Close open windows" script for shutdown.

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

#21 2016-06-03 01:44:08

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 5,740
Website

Re: "Close open windows" script for shutdown.

If you add a script to bl-exit, be sure not to get recursive!
ie bl-exit calls shutdown.sh which calls bl-exit...


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
In case you forget, the rules.

Online

#22 2016-06-03 03:12:52

Eraph
Member
From: /au/qld/bne
Registered: 2016-02-29
Posts: 282
Website

Re: "Close open windows" script for shutdown.

Yep. My intention is to symlink your script at the start of this thread in those directories. No change to bl-exit.


Lenovo IdeaPad Yoga 13 | BunsenLabs Hydrogen (x64)
Intel Core i7-3537U | Intel HD4000 | 8GB DDR3 | 256GB SSD

Offline

#23 2016-06-03 05:01:10

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 5,740
Website

Re: "Close open windows" script for shutdown.

^Interesting idea. One potential problem: that script in rc0.d will be executed by root, while close_all_windows is a user script, executed while X is running and with the related X environment variables and cookies for that user's session. It might not work at all run by root, and the user's X session might have been closed down by the time those rc0.d scripts sre run. Too late.


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
In case you forget, the rules.

Online

#24 2016-06-03 05:53:10

Eraph
Member
From: /au/qld/bne
Registered: 2016-02-29
Posts: 282
Website

Re: "Close open windows" script for shutdown.

Good call! So it's not the system shut-down event we want to hook into, but the user log-out event. Maybe this is more what we're after?


Lenovo IdeaPad Yoga 13 | BunsenLabs Hydrogen (x64)
Intel Core i7-3537U | Intel HD4000 | 8GB DDR3 | 256GB SSD

Offline

#25 2016-06-03 23:45:30

ohnonot
...again
Registered: 2015-09-29
Posts: 3,917
Website

Re: "Close open windows" script for shutdown.

i think you have to ask yourself to what event the "Close all windows" script is connected.
it is not connected to shutdown.
it isn't even necessarily connected to logout.
most appropriately it is connected to ending the graphical session.
i'm sure there's some systemd event that corresponds to that. that's where you'd have to hook the script in.

btw, it is perfectly possible to run systemd services as a particular user.

and please don't confuse sysvinit and systemd - on a curent debian stable system they are mixed together somehow, but it is all going towards systemd in the end.

Offline

Board footer

Powered by FluxBB