Skip to content

Instantly share code, notes, and snippets.

@pablox-cl
Created August 1, 2011 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pablox-cl/1119237 to your computer and use it in GitHub Desktop.
Save pablox-cl/1119237 to your computer and use it in GitHub Desktop.
Bash script that throws a message when battery is low
#!/bin/bash
# Written by Pablo Olmos de Aguilera C. <pablo at glatelier dot org>
LIMIT=1000
MESSAGE='¡Queda poca batería! ¡Corre a buscar el cargador!'
BAT_DIR='/proc/acpi/battery/BAT0'
echo 'running...'
initial=$(cat ${BAT_DIR}/* | grep 'remaining' | awk '{ print $3 }')
#rate=$(cat /proc/acpi/battery/BAT0/* | grep 'rate' | awk '{ print $3 }')
#hours_left=$(( $initial / $rate ))
#echo "Ahora hay unos ${initial} mWh... a un supuesto gasto de ${rate} por hora,\
# por lo tanto -en teoría- debería haber batería para ${hours_left} horas"
if (cat ${BAT_DIR}/* | sed 's/ //g' | uniq | grep 'present:no' > /dev/null); then
echo 'no battery present...exiting'
exit 1
fi
while :; do
remaining=$(cat ${BAT_DIR}/* | grep 'remaining' | awk '{ print $3 }')
if (($remaining < $LIMIT)); then
notify-send "${MESSAGE}"
echo $remaining
sleep 540
fi
sleep 60
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment