SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2011-05-03 12:01:08

Tunafish
#! Die Hard
From: the Netherlands
Registered: 2010-03-07
Posts: 1,204

Looking for a more friendly shutdown...

When i shutdown Ubuntu or (long ago) Windows, it would ask me to save unsaved documents. Is it possible to get this behavior in #! as well?

Thanks,
Tuna

Offline

Help fund CrunchBang, donate to the project!

#2 2011-05-04 05:45:12

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: Looking for a more friendly shutdown...

Easy. Make yourself a "shutdown.sh" script. Put in it whatever command you were previously using to shut down, and change your menu & keyboard to point to it. Then precede the shutdown command in the script with a line to call, for example, zenity, to confirm. eg:

zenity --question --title='Shutdown?' --text='Please save any documents before continuing. Press "Yes" when ready, or "No" to cancel shutdown.' || exit 

Offline

#3 2011-05-04 09:42:43

Tunafish
#! Die Hard
From: the Netherlands
Registered: 2010-03-07
Posts: 1,204

Re: Looking for a more friendly shutdown...

Last edited by Tunafish (2011-05-04 09:57:09)

Offline

#4 2011-05-04 10:11:14

Tunafish
#! Die Hard
From: the Netherlands
Registered: 2010-03-07
Posts: 1,204

Re: Looking for a more friendly shutdown...

I did this:

#!/bin/bash
ob_windows_list=(`wmctrl -l | awk '{print $4}'`)

for i in ${ob_windows_list[@]}
do
    wmctrl -c  $i
done
 
gdm-control --shutdown
openbox --exit

This will ask to save documents if necessary, but it doesn't wait for me to do so. How do i solve this?

Even better would be to close the last window, wait till it's done, than close the next, etc.

Last edited by Tunafish (2011-05-04 11:13:37)

Offline

#5 2011-05-05 04:36:04

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: Looking for a more friendly shutdown...

#!/bin/bash
ob_windows_list=(`wmctrl -l | awk '{print $4}'`)

for i in ${ob_windows_list[@]}
do
    wmctrl -c  $i
done
 
gdm-control --shutdown
openbox --exit

Last edited by johnraff (2011-05-05 04:54:24)

Offline

#6 2011-05-05 08:27:01

Tunafish
#! Die Hard
From: the Netherlands
Registered: 2010-03-07
Posts: 1,204

Re: Looking for a more friendly shutdown...

So... now i have this:

#!/bin/bash
#~/bin/safeshutdown
# depens on wmctrl

ob_windows_list=(`wmctrl -l | awk '{print $4}'`)

openwindows=$(wmctrl -l | grep -v " N/A " | wc -l)
while [ $openwindows -gt 0 ]
do
    for i in ${ob_windows_list[@]}
    do
        wmctrl -c $i
    done
    openwindows=$(wmctrl -l | grep -v " N/A " | wc -l)
done

gdm-control --shutdown
openbox --exit

Terrible script to test as it closes all your windows all the time sad 
(of course i left out the shutdown part during testing...)

But i still welcome better ideas than mine. How is shutdown done in Ubuntu or Debian Mint? That might be interesting...

Last edited by Tunafish (2011-05-05 15:41:20)

Offline

#7 2011-05-06 05:38:15

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: Looking for a more friendly shutdown...

wmctrl -lp | awk '{print $5 " " $3}'

gives you the pid of each window after its name. Don't know if that would be any use...

Offline

#8 2011-06-10 05:59:42

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: Looking for a more friendly shutdown...

ob_windows_list=(`wmctrl -l | awk '!/Conky|tint2|ADeskBar/{print $4}'`)

Offline

#9 2011-06-10 07:48:07

Tunafish
#! Die Hard
From: the Netherlands
Registered: 2010-03-07
Posts: 1,204

Re: Looking for a more friendly shutdown...

ob_windows_list=(`wmctrl -l | awk '!/Conky|tint2|ADeskBar/{print $4}'`)

Offline

#10 2011-06-10 13:09:45

slapfish
#! Die Hard
From: Athens, Greece
Registered: 2009-10-22
Posts: 601

Re: Looking for a more friendly shutdown...

I was playing around with the script and the best code I came up with is this:

ob_windows_list=$(wmctrl -l | awk '!/Conky|tint2|ADeskBar/{print $4}')
openwindowsnumber=$(wmctrl -l | awk '!/Conky|tint2|ADeskBar/' | wc -l)
openwindowsnumbertest=$(wmctrl -l | awk '!/Conky|tint2|ADeskBar/' | wc -l)

while [ $openwindowsnumber -gt 0 ]; 
do
    for i in ${ob_windows_list[@]}
    do
        wmctrl -R $i
        wmctrl -c $i
        openwindowsnumbertest=$(wmctrl -l | awk '!/Conky|tint2|ADeskBar/' | wc -l)
        
        until [ $openwindowsnumbertest -lt $openwindowsnumber ]; do
                openwindowsnumbertest=$(wmctrl -l | awk '!/Conky|tint2|ADeskBar/' | wc -l)
        done
        
    done
    openwindowsnumber=$(wmctrl -l | awk '!/Conky|tint2|ADeskBar/' | wc -l)
done

This way the windows are closing one by one and if saving or verification is needed the script waits. The problem is that if you choose to cancel the closing, the script should be trappted in an infinite loop. However on my tests it appears to be finishing. I'm guessing it counts the confirmation window as +1 window that tries to close and when you actually cancel the action and close the +1 window the loop test gets true and the script does finish.

I now have (or someone else) to find a workaround for the canceling thing...

Offline

#11 2011-06-10 13:16:17

slapfish
#! Die Hard
From: Athens, Greece
Registered: 2009-10-22
Posts: 601

Re: Looking for a more friendly shutdown...

I'm getting closer, but since I have almost no experience with scripting and programing, I'm getting a little bit confused on what it's going on. Anyway, I added one line and the script will ask one more time for any canceled closing action. But if you again chose to cancel  the closing then it stops. Here is the script:

ob_windows_list=$(wmctrl -l | awk '!/Conky|tint2|ADeskBar/{print $4}')
openwindowsnumber=$(wmctrl -l | awk '!/Conky|tint2|ADeskBar/' | wc -l)
openwindowsnumbertest=$(wmctrl -l | awk '!/Conky|tint2|ADeskBar/' | wc -l)

while [ $openwindowsnumber -gt 0 ]; 
do
    for i in ${ob_windows_list[@]}
    do
        wmctrl -R $i
        wmctrl -c $i
        openwindowsnumbertest=$(wmctrl -l | awk '!/Conky|tint2|ADeskBar/' | wc -l)
        until [ $openwindowsnumbertest -lt $openwindowsnumber ]; do
            openwindowsnumbertest=$(wmctrl -l | awk '!/Conky|tint2|ADeskBar/' | wc -l)
        done
    done
    ob_windows_list=$(wmctrl -l | awk '!/Conky|tint2|ADeskBar/{print $4}')
    openwindowsnumber=$(wmctrl -l | awk '!/Conky|tint2|ADeskBar/' | wc -l)
done

EDIT: I was wrong about the loop. It actually gets trapped in an infinite loop if you keep canceling the closing and It might also not waiting for one to get canceled. I'll have to look into it a little more (or much more).

Last edited by slapfish (2011-06-10 13:24:26)

Offline

#12 2011-06-10 17:51:51

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: Looking for a more friendly shutdown...

ob_windows_list=($(wmctrl -l | awk '!/'$IGNORE'/{print $1}'))

Offline

#13 2011-06-10 18:20:46

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: Looking for a more friendly shutdown...

It's past 3am and I must get to bed, but is this getting any closer?

#!/bin/bash
# close_all_windows
# depends on wmctrl

IGNORE='Conky|tint2|ADeskBar|Desktop'
ob_windows_list=($(wmctrl -lp | awk '!/'$IGNORE'/{print $1 "|" $3}')) # get ids and pids, separated by pipe symbol
for i in "${ob_windows_list[@]}"
do
    wmctrl -i -R ${i%|*}
    echo "closing ${i%|*}"
    wmctrl -i -c ${i%|*}
    while kill -0 ${i#*|} >/dev/null 2>&1
    do
        echo "waiting for ${i#*|}"
        sleep 0.5
    done
done

exit

The "echo" bits are just for debugging.

Offline

#14 2011-06-11 17:23:00

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: Looking for a more friendly shutdown...

Had a rethink about using the window's pid to test if it's still open or not - there are a couple of problems:
1) Some windows don't let wmctrl know their pid - conky and tint2 in particular - and the pid shows as "0". OK if they're being ignored anyway, but if a window with pid = 0 is closed the script will hang in an endless loop waiting for 'kill -0 0' to return 1 which it never does.
2) Sometimes windows share the same pid, eg if you're using urxvtd then all the urxvtc terminal windows have the same pid, so again the script will hang after closing the first one because the pid doesn't go away.

So how about staying with the window IDs and checking the output of 'wmctrl -l' with grep instead? If you already know the window's ID number is $i then

wmctrl -l | grep "$i" >/dev/null 2>&1

will return 0 if the window is open and 1 if it's closed. It's slower than using kill with the pid, but maybe it doesn't really matter.

That gives us a close_all_windows script something like this:

#!/bin/bash
# close_all_windows
# depends on wmctrl

IGNORE='Conky|tint2|ADeskBar|Desktop' # leave these windows alone

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

This seems to be working nicely for me now, and I've added a call to this script from my shutdown script. I think it's worth keeping it as a separate script in the modular unix tradition, because there might be other times when you want to tidy everything away - like before doing a backup for example. cool

Offline

#15 2011-06-12 10:43:36

Tunafish
#! Die Hard
From: the Netherlands
Registered: 2010-03-07
Posts: 1,204

Re: Looking for a more friendly shutdown...

wmctrl -l | grep "$i" >/dev/null 2>&1

Offline

#16 2011-06-13 10:43:23

slapfish
#! Die Hard
From: Athens, Greece
Registered: 2009-10-22
Posts: 601

Re: Looking for a more friendly shutdown...

#!/bin/bash
# safegdm-control for Shutdown/reboot/logout
# depens on wmctrl

case $1 in
   --shutdown) (gdm-control --shutdown) ;;
   --reboot) (gdm-control --reboot) ;;
   --logout) ;;
   *) (echo "Usage: safegdm-control { --shutdown | --reboot | --logout }")
        exit;;
esac

IGNORE='Conky|ADeskBar|Desktop' # leave these windows alone

ob_windows_list=($(wmctrl -l |  grep -v " N/A " | 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
        echo $i
    done
done

openbox --exit 

Offline

#17 2011-06-13 17:44:54

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: Looking for a more friendly shutdown...

Offline

#18 2011-06-13 20:23:09

slapfish
#! Die Hard
From: Athens, Greece
Registered: 2009-10-22
Posts: 601

Re: Looking for a more friendly shutdown...

Offline

#19 2011-06-13 21:07:26

Tunafish
#! Die Hard
From: the Netherlands
Registered: 2010-03-07
Posts: 1,204

Re: Looking for a more friendly shutdown...

Offline

#20 2011-06-14 18:22:32

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: Looking for a more friendly shutdown...

IGNORE=' Conky($|[ \t])| tint2($|[ \t])| ADeskBar($|[ \t])| Desktop($|[ \t])'

Last edited by johnraff (2011-06-15 04:31:51)

Offline

#21 2011-07-07 04:25:13

anonymous
The Mystery Member
From: Arch Linux Forums
Registered: 2008-11-29
Posts: 9,419

Re: Looking for a more friendly shutdown...

gnome-session-save --shutdown-dialog

Offline

#22 2011-07-08 11:06:08

Dev
Member
Registered: 2011-07-08
Posts: 27

Re: Looking for a more friendly shutdown...

Have you noticed that most applications, regardless whether they're for Windows or Linux tend to add an asterisk before or after the name of the application or the open document (whichever is shown in the titlebar).

Cases:
gedit (After opened document's name)
Blender (Before application name i.e.*Blender (path_to_document))

You could use this to determine whether the application has unsaved data or not.

Offline

#23 2011-07-08 11:14:36

Tunafish
#! Die Hard
From: the Netherlands
Registered: 2010-03-07
Posts: 1,204

Re: Looking for a more friendly shutdown...

Offline

#24 2011-07-08 11:42:36

Dev
Member
Registered: 2011-07-08
Posts: 27

Re: Looking for a more friendly shutdown...

Offline

Help fund CrunchBang, donate to the project!

#25 2011-07-09 05:06:31

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: Looking for a more friendly shutdown...

...but won't all the apps that are smart enough to display that asterisk already be putting up a "save data?" box when wmctrl tells them to close?

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.
Server: bleh

Debian Logo