You are not logged in.
Pages: 1
Hi all! I'm sure this one has been done to death, but with. It's a script to monitor a laptop battery and react accordingly. I wrote this for use with FVWM, to send out warning notices using libnotify when the battery gets too low. Since my laptop is quite old, the battery runs out quite quickly after getting to 25% capacity. The script backgrounds itself on being run. It starts when I begin an FVWM session, and I have FVWM kill the script when I logout.
Being a script it's easy to tweak to your particular requirements. The example below just warns, but it would be easy to have the script suspend or hibernate the system, or change the percentage charge at which the script performs different actions. I hope it's useful.
#!/bin/bash
#
# battery check script
# ====================
# Designed to be run continuously while WM is running
# Requires libnotify to be installed
#
ICONPATH=/home/rob/.fvwm/icons/24x18
BATDIR=/sys/class/power_supply/BAT0
NOTIFYSEND=/usr/bin/notify-send
if [ ! -x $NOTIFYSEND ]
then
exit 0
fi
# check every $CHECK seconds
CHECK=60
( while true
do
BATSTATE=`cat ${BATDIR}/status`
# Note valid BATSTATE values are: Charged, Charging, and Discharging
if [ "$BATSTATE" = "Discharging" ]
then
REM_CAP=`cat ${BATDIR}/charge_now`
FULL_CAP=`cat ${BATDIR}/charge_full`
CHARGE=`echo $(( $REM_CAP * 100 / $FULL_CAP ))`
if [ "$CHARGE" -lt "25" ]
then
$NOTIFYSEND -i $ICONPATH/battery_critical.png -u critical -t 30000 "battery critical" "Shutdown or connect power now"
sleep $CHECK
continue
fi
if [ "$CHARGE" -lt "30" ]
then
$NOTIFYSEND -i $ICONPATH/battery_low.png -u normal -t 20000 "battery low" "Connect power soon"
fi
sleep $CHECK
elif [ "$BATSTATE" = "Charged" ]
then
$NOTIFYSEND -i $ICONPATH/battery_full.png -u normal -t 20000 "battery fully charged" "Disconnect from power"
sleep $CHECK
else
sleep $CHECK
fi
done
) &
# end of file
Edited 21/10/2011 to use /sys/class/power_supply/BAT0 instead of /proc/acpi/battery/BAT0
Last edited by Painless (2011-10-21 12:48:05)
Offline
Pages: 1