Skip to content

Instantly share code, notes, and snippets.

@luca-m
Last active December 19, 2015 11:19
Show Gist options
  • Select an option

  • Save luca-m/5947229 to your computer and use it in GitHub Desktop.

Select an option

Save luca-m/5947229 to your computer and use it in GitHub Desktop.
Lightweight battery monitor.Using lightweight desktop environment such as awesome, i3 or xmonad may be really comfortable for certain aspect, but there are also few thing that you may miss from a regular desktop environment.This is the case of some kind of warning pop-up which indicates you that your battery is going dead. How many times do you …
#!/bin/bash
#
# Print battery warning
#
VERBOSE=0
SUSPEND_CMD=/home/stk/.i3/suspend.sh
TIMEOUT=120
DEVNAME='/org/freedesktop/UPower/devices/battery_BAT0'
ALERT_THR=15
CRITICAL_THR=10
SUSPEND_THR=5
function checkBattery (){
batteryLevel=`upower -i $DEVNAME |grep 'percentage' |grep -o '[0-9]*[\.0-9]*' | cut -d '.' -f1`
state=`upower -i $DEVNAME | grep "state" | cut -d ':' -f2 | grep -o "[a-z]*"`
if [ $VERBOSE -eq 1 ]
then
echo "Battery level: $batteryLevel"
echo "Battery state: $state"
fi
if [ "$state" == "discharging" ]
then
if [ "$SUSPEND_THR" -gt "$batteryLevel" ]
then
notify-send -u normal "Battery Almost Dead" "Level=$batteryLevel%!\nSuspending in $TIMEOUT sec."
sleep $TIMEOUT
exec $SUSPEND_CMD
elif [ "$CRITICAL_THR" -gt "$batteryLevel" ]
then
notify-send -u critical "Very Low Battery" "Level=$batteryLevel%!\nHurry up or I will suspend your session!"
elif [ "$ALERT_THR" -gt "$batteryLevel" ]
then
notify-send -u normal "Low Battery" "Lever=$batteryLevel%"
fi
fi
}
if [ "$1" == "--daemon" ];
then
while true
do
checkBattery
sleep $TIMEOUT
done
else
checkBattery
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment