#!/usr/local/bin/bash LOG=$HOME/battery.log FLAG=0 SHUTFLAG=0 SLEEP=30 while : do # state of the battery charger AC=`apm | grep -m1 AC | awk '{print $4}'` # charge in percent LIFE=`apm | grep -m 1 life | awk '{print $4}' | sed 's/%//'` # apm's estimate of remaining battery life time TIME=`apm | grep -m 1 time | awk '{print $4}'` # use sysctl to find the power consumption in mW RATE=`sysctl -n hw.acpi.battery.rate` #and CPU Freq FREQ=`sleep 1; sysctl -n dev.cpu.0.freq` # echo "$FREQ" LIGHT=`backlight -q` # echo $LIGHT MON=`$HOME/bin/getmon && cat $HOME/bin/monx | grep Monitor | awk '{print $NF}'` echo $MON # If we're on battery we'll log the readings if [ $AC == "off-line" ] then echo "`date "+%Y-%m-%d %H:%M:%S"`; $TIME; $LIFE%; $RATE mW; $FREQ MHz; LCD:$LIGHT; Screen $MON ; $AC" >> $LOG else if [ $SHUTFLAG == 1 ] then echo "`date` Cancelling Shutdown" >> $LOG sudo killall shutdown SHUTFLAG=0 else echo "`date "+%Y-%m-%d %H:%M:%S"`; $TIME; $LIFE%; $RATE mW; $FREQ MHz; LCD:$LIGHT; Screen $MON; $AC" >> $LOG fi fi # if we're over 30% we'll do nothing & reset the low battery notification flag if [ $LIFE -gt 30 ] then FLAG=0 # echo "Battery ok $LIFE charger $AC" else # otherwise we're getting low, so set send a notification # if the flag isn't yet set (i.e. it's the first one) # echo "Battery Low $LIFE Charger $AC" if [ $FLAG != 1 ] then SLEEP=5 notify-send -t 10000 -i battery-level-40 "BATTERY" "Low $LIFE Charger $AC" echo "`date "+%Y-%m-%d %H:%M:%S"` FIRST WARNING SENT..." >> $LOG # then set the flag to prevent further notifications FLAG=1 fi fi if [ $LIFE -lt 20 ] then if [ $AC == "off-line" ] then # send a notification notify-send -t 10000 -i battery-level-20 "BATTERY" "CRITICAL.... $LIFE Charger $AC" # echo "Initiating Shutdown...." # and log it in the logfile echo "`date "+%Y-%m-%d %H:%M:%S"` Critical Message sent" >> $LOG # Then add code to do the shutdown # sudo shutdown -p now (or in x minutes?) #if [ $SHUTFLAG == 0 ] #then #sudo shutdown -p +5 # SHUTFLAG=1 #fi fi fi # if we're below 15% we need to think about shutting down if [ $LIFE -lt 15 ] then if [ $AC == "off-line" ] then # send a notification notify-send -t 10000 -i battery-level-10 "BATTERY" "CRITICAL.... $LIFE Charger $AC" # echo "Initiating Shutdown...." # and log it in the logfile echo "`date`;$TIME;$LIFE;$RATE;$AC;Shutdown Initiated" >> $LOG # Then add code to do the shutdown # sudo shutdown -p now (or in x minutes?) if [ $SHUTFLAG == 0 ] then sudo shutdown -p +1 SHUTFLAG=1 fi fi fi sleep $SLEEP done