ShutdownCounter 3.x Pet for xfce Battery Monitor

Miscellaneous tools
Post Reply
Message
Author
User avatar
johnywhy
Posts: 879
Joined: Sat 20 Aug 2011, 14:52

ShutdownCounter 3.x Pet for xfce Battery Monitor

#1 Post by johnywhy »

What it does: this pet automatically shuts down your computer when your battery reaches critical level. It displays a 60-second (user-adjustable) timer, giving the user a chance to save their files before shutdown. Version 3 adds an animated gauge. Version 2.0 added the option to cancel the shutdown.
Image

Why use it? it's handy if you're in the habit of running your battery until it's totally dead, resulting in a hard, instant, dirty shutdown-- which can't be too healthy for your hardware or your puppy OS :)

Requirements:
this pet requires xfce4-battery-plugin.
Image
Tested on xSlacko-b7 and Vlina-r2. Hopefully, other peeps here can report if it works on other puppies.

How to use it? nothing to configure, just install it. To see how it's set up, pop open your Battery Monitor config panel by right-clicking the Battery Monitor tray icon, and click "Properties". You'll see that "Critical Battery Action" has been set to "Run Command", and xfce4-shutdowncounter has been loaded into the "Command" field. The command will be executed when your battery discharges to critical level-- it's a script which displays the graphic shutdown alert shown above. You don't have to do anything else.
Image

Optionally, the timeout seconds can be customized by entering your desired timeout seconds in the battery monitor config 'Command' box, after "xfce4-shutdowncounter":
Image

How to test: Save and close all work. open a CLI terminal window, and enter xfce4-shutdowncounter. You should see the progress gauge. Allow the progress gauge 60 seconds to run out. Then your machine should shutdown.

Gotchas: This pet has one gotcha. The xfce panel has a watchdog, which checks for faulty panel applets which repeatedly restart within a small time-window. Therefor, if you install and uninstall this pet within 1 minute, then you will see this alert:
Image

don't be alarmed, just click "Execute" to restore the battery monitor. If you click "Remove", no worries: just manually re-add the battery monitor to your panel using xfce panel preferences. No settings will be corrupted.

enjoy! :)


Technical Details

PQ - Puppiness Quotient: No known bugs, bloat-free, well commented, no extra depends. 4-line installer, 9-line bash script, 4-line Uninstaller. No other code. Executes only on low battery. Triggered by official xfce process, not a listener. No adware, malware, viruses, or bots, just does what it 'posed ta do. All code documented below.

How does it work? This xfce4-shutdowncounter script displays the dialog-box powerdown progress gauge above:

Code: Select all

#!/bin/sh

# get script path, so it can find icon
mypath="`dirname $0`"

# get optional user waittime. Default to 60 seconds. 
waittime=${1:-60}

# get step size for gauge by converting waittime to 100%
stepsize=$((100 / $waittime))

# loop once per second, updating progress bar as you go. 
(for (( percent=$stepsize; percent <= 100; percent+=$stepsize ))
	do
	# update progress bar
	echo "$percent"

	sleep 1
	done) |

# display gauge
Xdialog --title "Battery Monitor" --icon "$mypath/xfce4-battery-critical.xpm" --gauge "Battery dangerously low!

This computer will power down in $waittime seconds.

To cancel the shutdown, close this window.


% remaining:" 13 60

# if gauge was not closed prematurely, then shutdown the computer.  
[ "$?" = 0 ] &&	wmpoweroff
What does the Installer do? The installer enters settings into the Battery Monitor panel (as shown above), and puts the shutdown script into your bin.

The installer enters settings into the Battery Monitor panel by writing values to the Battery Monitor config file:
/root/.config/xfce4/panel/battery-10.rc

Before installation, the config file includes the following:

Code: Select all

...
action_on_critical=1
command_on_critical=
After installation, the entries above are rewritten as:

Code: Select all

...
action_on_critical=2
command_on_critical=xfce4-shutdowncounter
here's the installer:

Code: Select all

#!/bin/bash

# set battery monitor to run a command on critical level, and write new critical action command to battery config. then kill battery monitor to apply new settings. It will restart automatically. 

# set battery config to run a command on critical
sed -i 's,^\(action_on_critical=\).*,\1'2',' /root/.config/xfce4/panel/battery-10.rc

# add command to battery config file. 
sed -i 's,^\(command_on_critical=\).*,\1'xfce4-shutdowncounter',' /root/.config/xfce4/panel/battery-10.rc

# kill battery monitor. will restart automatically.
kill -9 $(ps -ef | grep libbattery.so | grep -v grep | awk '{ print $2 }')
What does the Uninstaller do? the uninstaller removes everything:

Code: Select all

#!/bin/bash

# clear the critical action command from battery monitor, set run default standard alert. then kill battery monitor to apply the change. It will restart automatically. 

# remove command from battery config. 
sed -i 's,^\(command_on_critical=\).*,\1'',' /root/.config/xfce4/panel/battery-10.rc

# set battery config to display standar alert on critical
sed -i 's,^\(action_on_critical=\).*,\1'1',' /root/.config/xfce4/panel/battery-10.rc

# kill battery monitor. will restart automatically.
kill -9 $(ps -ef | grep libbattery.so | grep -v grep | awk '{ print $2 }')
Implementation details:
- xfce4-battery-plugin does not use xfce's xfconf system. Therefor, the installer for this pet must kill the battery monitor temporarily for the new settings to take effect. The battery monitor terminates and restarts so quickly you may not even see it happen. If by chance the battery monitor dev (or someone) makes an xfconf patch for the battery monitor, then the kill should not be necessary, and the panel alert mentioned above will never happen.

- currently using xdialog, because simple. Drawback: only displays a percentage gauge, so wait-time has to be scaled to 100%.

- i might have used the pre-existing /etc/acpi/actions/acpi_poweroff.sh as my shutdown counter. acpi_poweroff has more features, but that script is about 175 lines of code, and my shutdown counter is only 9 lines of code. I prefer simple/small :)

Changes:
- 3.2 is path fix for icon.
- 3.3 works with different xfce install-id's of xfce4-battery-plugin config file., and handles spaces in directory names.
Attachments
xfce4-ShutdownCounter-3.3.pet
(5.32 KiB) Downloaded 282 times
Last edited by johnywhy on Mon 08 Jun 2015, 10:59, edited 46 times in total.

User avatar
johnywhy
Posts: 879
Joined: Sat 20 Aug 2011, 14:52

#2 Post by johnywhy »

New version 2.0

This pet adds a "Cancel" button to the countdown alert, giving the user the chance to plug in their computer and cancel shutdown.

Pet posted in OP.
[b]Now[/b]: X-Tahr 2.0! StretchDog! DevuanDog!
[b]Tops[/b]: TarhNOP Vlina-R2 Racy
[b]Used[/b]: Puppeee Precise Lucid Wary Tahrpup Quirky Slacko MacPup Saluki Puppy Studio LxPupTarh Lina-Lite Lina
[i]i ♥ Puppy[/i]

User avatar
johnywhy
Posts: 879
Joined: Sat 20 Aug 2011, 14:52

#3 Post by johnywhy »

Version 3 adds an animated gauge.
[b]Now[/b]: X-Tahr 2.0! StretchDog! DevuanDog!
[b]Tops[/b]: TarhNOP Vlina-R2 Racy
[b]Used[/b]: Puppeee Precise Lucid Wary Tahrpup Quirky Slacko MacPup Saluki Puppy Studio LxPupTarh Lina-Lite Lina
[i]i ♥ Puppy[/i]

User avatar
johnywhy
Posts: 879
Joined: Sat 20 Aug 2011, 14:52

#4 Post by johnywhy »

3.2 is fix for icon path.
[b]Now[/b]: X-Tahr 2.0! StretchDog! DevuanDog!
[b]Tops[/b]: TarhNOP Vlina-R2 Racy
[b]Used[/b]: Puppeee Precise Lucid Wary Tahrpup Quirky Slacko MacPup Saluki Puppy Studio LxPupTarh Lina-Lite Lina
[i]i ♥ Puppy[/i]

User avatar
Geoffrey
Posts: 2355
Joined: Sun 30 May 2010, 08:42
Location: Queensland

#5 Post by Geoffrey »

Your script presumes that the battery plugin is plugin-10, this will fix that by extracting the battery plugin number, mine is "11", so the script as is now don't work for me.

Code: Select all

PLUGIN=`cat ~/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-panel.xml | grep battery | sed -r 's/[plugin-]+//g' | awk -F'"' '{print $2}'`

Code: Select all

/root/.config/xfce4/panel/battery-"$PLUGIN".rc
[b]Carolina:[/b] [url=http://smokey01.com/carolina/pages/recent-repo.html]Recent Repository Additions[/url]
[img]https://dl.dropboxusercontent.com/s/ahfade8q4def1lq/signbot.gif[/img]

User avatar
johnywhy
Posts: 879
Joined: Sat 20 Aug 2011, 14:52

#6 Post by johnywhy »

Geoffrey wrote:Your script presumes that the battery plugin is plugin-10, this will fix that by extracting the battery plugin number, mine is "11", so the script as is now don't work for me.

Code: Select all

PLUGIN=`cat ~/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-panel.xml | grep battery | sed -r 's/[plugin-]+//g' | awk -F'"' '{print $2}'`

Code: Select all

/root/.config/xfce4/panel/battery-"$PLUGIN".rc
thx G! i will check this out and integrate!
[b]Now[/b]: X-Tahr 2.0! StretchDog! DevuanDog!
[b]Tops[/b]: TarhNOP Vlina-R2 Racy
[b]Used[/b]: Puppeee Precise Lucid Wary Tahrpup Quirky Slacko MacPup Saluki Puppy Studio LxPupTarh Lina-Lite Lina
[i]i ♥ Puppy[/i]

User avatar
johnywhy
Posts: 879
Joined: Sat 20 Aug 2011, 14:52

#7 Post by johnywhy »

Geoffrey wrote:Your script presumes that the battery plugin is plugin-10,... mine is "11", so the script as is now don't work for me.
G, i prefer keeping things as simple as possible. This command is nearly identical to my current code, but uses a * wildcard in the version-number position. It will work with any version number. It will apply settings to battery-10.rc OR battery-11.rc. It does not require additional variables or additional lines of code, which your suggestion does.

Code: Select all

sed -i 's,^\(command_on_critical=\).*,\1'banana',' /root/.config/xfce4/panel/battery-*.rc
If a user has BOTH versions installed, it will apply the new settings to both versions-- i'm fine with that. But, why would a user have both versions installed? I'm comfortable with this approach.
Last edited by johnywhy on Tue 12 May 2015, 16:55, edited 1 time in total.
[b]Now[/b]: X-Tahr 2.0! StretchDog! DevuanDog!
[b]Tops[/b]: TarhNOP Vlina-R2 Racy
[b]Used[/b]: Puppeee Precise Lucid Wary Tahrpup Quirky Slacko MacPup Saluki Puppy Studio LxPupTarh Lina-Lite Lina
[i]i ♥ Puppy[/i]

User avatar
Geoffrey
Posts: 2355
Joined: Sun 30 May 2010, 08:42
Location: Queensland

#8 Post by Geoffrey »

johnywhy wrote: i prefer keeping things as simple as possible. This command is nearly to my current code, but uses a * wildcard in the version-number position. It will work with any version number. It will apply settings to battery-10.rc OR battery-11.rc. It does not require additional variables or additional lines of code, which your suggestion does.
Cool, that will do, the battery plugin can change it's battery-x.rc number depending on if it's removed and reinstalled.
[b]Carolina:[/b] [url=http://smokey01.com/carolina/pages/recent-repo.html]Recent Repository Additions[/url]
[img]https://dl.dropboxusercontent.com/s/ahfade8q4def1lq/signbot.gif[/img]

User avatar
johnywhy
Posts: 879
Joined: Sat 20 Aug 2011, 14:52

#9 Post by johnywhy »

Geoffrey wrote:...depending on if it's removed and reinstalled.
sorry, i don't follow that part.

thx!
[b]Now[/b]: X-Tahr 2.0! StretchDog! DevuanDog!
[b]Tops[/b]: TarhNOP Vlina-R2 Racy
[b]Used[/b]: Puppeee Precise Lucid Wary Tahrpup Quirky Slacko MacPup Saluki Puppy Studio LxPupTarh Lina-Lite Lina
[i]i ♥ Puppy[/i]

User avatar
johnywhy
Posts: 879
Joined: Sat 20 Aug 2011, 14:52

#10 Post by johnywhy »

3.3 works with different xfce install-id's of xfce4-battery-plugin config file., and handles spaces in directory names.
Last edited by johnywhy on Wed 13 May 2015, 06:07, edited 1 time in total.
[b]Now[/b]: X-Tahr 2.0! StretchDog! DevuanDog!
[b]Tops[/b]: TarhNOP Vlina-R2 Racy
[b]Used[/b]: Puppeee Precise Lucid Wary Tahrpup Quirky Slacko MacPup Saluki Puppy Studio LxPupTarh Lina-Lite Lina
[i]i ♥ Puppy[/i]

User avatar
Geoffrey
Posts: 2355
Joined: Sun 30 May 2010, 08:42
Location: Queensland

#11 Post by Geoffrey »

johnywhy wrote:
Geoffrey wrote:...depending on if it's removed and reinstalled.
sorry, i don't follow that part.
I mean that if the battery panel item is removed and added again it can be assigned a different plugin number, depending on what other plugins are added also.

The "plugin-19" would define battery-19.rc .
</property>
<property name="plugin-2" type="string" value="weather"/>
<property name="plugin-9" type="string" value="whiskermenu"/>
<property name="plugin-19" type="string" value="battery"/>
</property>
[b]Carolina:[/b] [url=http://smokey01.com/carolina/pages/recent-repo.html]Recent Repository Additions[/url]
[img]https://dl.dropboxusercontent.com/s/ahfade8q4def1lq/signbot.gif[/img]

User avatar
johnywhy
Posts: 879
Joined: Sat 20 Aug 2011, 14:52

#12 Post by johnywhy »

does xfce4-power-manager-1.2.0-i486-2 provide auto-shutdown on low battery? if so, my pet is not needed.

however, xfce4-power-manager-1.2.0-i486-2 does not work on my xSlacko, even tho downloaded from xSlacko ppm.
Geoffrey wrote:if the battery panel item is removed and added again it can be assigned a different plugin number, depending on what other plugins are added also.
i see, it's not version number, it's arbitrary based on other installed plugins.
[b]Now[/b]: X-Tahr 2.0! StretchDog! DevuanDog!
[b]Tops[/b]: TarhNOP Vlina-R2 Racy
[b]Used[/b]: Puppeee Precise Lucid Wary Tahrpup Quirky Slacko MacPup Saluki Puppy Studio LxPupTarh Lina-Lite Lina
[i]i ♥ Puppy[/i]

Post Reply