Last active
August 29, 2015 14:14
-
-
Save pankaj28843/dfcd69db11e7adeb7d72 to your computer and use it in GitHub Desktop.
Sends a notification if laptop battery is lower than a minimum level
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
while true; | |
do | |
# Minimum battery level in percentage. | |
# If remaning battery percentage is less than this then a notification will be shown. | |
MINIMUM_LEVEL=50; | |
# Determine battery status | |
BATTERY_STATUS=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "state" | sed -E 's/state://' | sed 's/^[ \t]*//;s/[ \t]*$//'); | |
# Calculate remaining battery | |
REMAING_BATTERY=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "percentage"|sed -r 's/([^0-9]*([0-9]*)){1}.*/\2/'); | |
if [ "$BATTERY_STATUS" = "discharging" ] | |
then | |
if [ $REMAING_BATTERY -lt $MINIMUM_LEVEL ] | |
then | |
# Send a notification | |
notify-send -u critical 'Low Battery' $REMAING_BATTERY' % battery is remaining. Please plug your charger.'; | |
fi | |
fi | |
# Sleep for 45 seconds and then recheck | |
sleep 45; | |
done; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment