Skip to content

Instantly share code, notes, and snippets.

@pankaj28843
Last active August 29, 2015 14:14
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 pankaj28843/dfcd69db11e7adeb7d72 to your computer and use it in GitHub Desktop.
Save pankaj28843/dfcd69db11e7adeb7d72 to your computer and use it in GitHub Desktop.
Sends a notification if laptop battery is lower than a minimum level
#!/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