Skip to content

Instantly share code, notes, and snippets.

@toanant
Created December 1, 2016 15:04
Show Gist options
  • Save toanant/eaf7d566dd2ee850149f8df517d6a4ac to your computer and use it in GitHub Desktop.
Save toanant/eaf7d566dd2ee850149f8df517d6a4ac to your computer and use it in GitHub Desktop.
#!/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=23;
# 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