Monday, October 19, 2009

Low Battery Warning Beep Script For Linux

#!/bin/bash

DIR="/sys/class/power_supply/BAT0"
STATUS=$(cat "$DIR/status")

if [ "$STATUS" == "Discharging" ]
then
        for PREFIX in "charge" "energy"
        do
                if [ -f "$DIR/${PREFIX}_now" ] && [ -f "$DIR/${PREFIX}_full" ]
                then
                        REMAIN=$(cat "$DIR/${PREFIX}_now")
                        FULL=$(cat "$DIR/${PREFIX}_full")
                        PRCT=$((100 * $REMAIN / $FULL))
                        if [ $PRCT -le 10 ]
                        then
                                mplayer /usr/share/sounds/bark.ogg
                        fi
                        break
                fi
        done
fi
Put that in your crontab and smoke it.

UPDATE: There is/may be a warn file in $DIR which you can use instead of 10/

No comments:

Post a Comment