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.

Revisions

  1. toanant created this gist Dec 1, 2016.
    24 changes: 24 additions & 0 deletions low-battery-notification.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    #!/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;