Be notified when critical battery level is reached

Ever happened to be focused on something and miss the fact that your laptop is running out of battery? And to lose your work?
Yesterday happened twice! "Really? Hmm... I need to fix this as soon as possible."
I googled a bit and this stackexchange post  popped up. Nice, great! But being notified even when you're charging your laptop it's not nice and I had to change the solution a bit:

[codesyntax lang="bash"]

cat .notify-send_setup

[/codesyntax]

#!/bin/bash
touch $HOME/.dbus/Xdbus
chmod 600 $HOME/.dbus/Xdbus
env | grep DBUS_SESSION_BUS_ADDRESS > $HOME/.dbus/Xdbus
echo 'export DBUS_SESSION_BUS_ADDRESS' >> $HOME/.dbus/Xdbus

exit 0

[codesyntax lang="bash"]

cat .battnotif

[/codesyntax]

#!/bin/bash
export DISPLAY=:0
XAUTHORITY=$HOME/.Xauthority

if [ -r "$HOME/.dbus/Xdbus" ]; then
    . "$HOME/.dbus/Xdbus"
fi

battery_level=$(acpi -b | grep -P -o '[0-9]+(?=%)')
STATUS=$(acpi -b | awk '{print $3'} | sed -e 's/,//g')

if [ $battery_level -le 15 ] && [ $STATUS == "Discharging" ]
then
    /usr/bin/notify-send -u critical "Battery low" "Battery level is ${battery_level}%!"
    echo 'batt low' >> $HOME/cron.log
fi

echo 'ran batt' >> $HOME/cron.log

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.