You are not logged in.
This is a little script I wrote for use on my laptop. I wanted to have some kind of visual cue to let me know when the battery charge gets below a certain level and this is what I came up with:
#!/bin/bash
# Configuration
interval=120 #in seconds
critical_level=10 #percent
icon="/usr/share/icons/Tango/48x48/devices/battery.png" #notification icon
battery_id="BAT1" #ACPI battery identifier
ac_adapter_id="ADP1" #ACPI power adapter identifier
while true
do
if [ "$(cat /proc/acpi/ac_adapter/$ac_adapter_id/state | grep -o off)" == "off" ]; then
battery_max=`cat /proc/acpi/battery/$battery_id/info | head -3 | tail -1 | awk '{print $4}'`
battery_current=`cat /proc/acpi/battery/$battery_id/state | head -5 | tail -1 | awk '{print $3}'`
battery_level=$((100*$battery_current/$battery_max))
[ $battery_level -le $critical_level ] && \
notify-send -u critical -i "$icon" -t 15000 \
"Battery level is low!" "Only $battery_level% of the charge remains."
fi
sleep $interval
done
This script uses notification-daemon (or notification-daemon-xfce) package. It can be configured by editing values in the "Configuration" section. I just autostart it with my openbox session. It all should be pretty self-explanatory otherwise.
I hope someone finds it useful.
EDIT: Some improvements to the script.
Last edited by fwojciec (2008-07-09 13:11:14)
Offline
thx, really cool i always forget to look after my battery percentage
maybe you can replace "BAT1" and "ADP1" with variables, so configuration would be easier
☃ Snowman ☃
Offline
laptop-mode tools has an option of running commands when battery charge reaches a certain level. You could just run notify-send from there.
The day Microsoft makes a product that doesn't suck, is the day they make a vacuum cleaner.
--------------------------------------------------------------------------------------------------------------
But if they tell you that I've lost my mind, maybe it's not gone just a little hard to find...
Offline
thx, really cool i always forget to look after my battery percentage
maybe you can replace "BAT1" and "ADP1" with variables, so configuration would be easier
Thanks I made changes according to your suggestion.
laptop-mode tools has an option of running commands when battery charge reaches a certain level. You could just run notify-send from there.
You mean in the auto-hibernate section? I use it as it was supposed to be used -- for hibernating -- and I think you can only set one command that's executed according to a specified battery level in laptop-mode-tools. Plus it would be sort of complicated, since displaying the notification depends on two conditions in this script -- specified battery level and power cord being disconnected.
Offline
Here's an alternative version of the script -- if anyone prefers it. It is slightly more minimalistic, but also depends on acpi package.
#!/bin/bash
# Configuration
interval=120 #in seconds
critical_level=10 #percent
icon="/usr/share/icons/Tango/48x48/devices/battery.png" #notification icon
while true
do
if [ "$(acpi -a | grep -o off)" == "off" ]; then
battery_level=`acpi -b | sed 's/.*[dg], //g;s/\%,.*//g'`
[ $battery_level -le $critical_level ] && \
notify-send -u critical -i "$icon" -t 15000 \
"Battery level is low!" "Only $battery_level% of the charge remains."
fi
sleep $interval
done
Last edited by fwojciec (2008-07-09 13:11:38)
Offline
Thanks for the correction It was one of the very first scripts I wrote so it's badly written, I admit. Your version is much better -- thanks for sharing.
Offline