Skip to content

Instantly share code, notes, and snippets.

@eze-kiel
Created August 6, 2021 12:34
Show Gist options
  • Save eze-kiel/f0b4ede024791271ba539ce9fa444af8 to your computer and use it in GitHub Desktop.
Save eze-kiel/f0b4ede024791271ba539ce9fa444af8 to your computer and use it in GitHub Desktop.
Bash script that notify if the battery level is too low. Initially made for a cronjob
#!/bin/bash
STATE=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | head -n 12 | tail -n1 | awk '{print $2}')
if [ "$STATE" == "charging" ]; then
exit 0
fi
PERCENT=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | head -n 21 | tail -n1 | awk '{print $2}' | cut -d '%' -f1)
if [ $PERCENT -lt 20 ]; then
export DISPLAY=:0
/usr/bin/notify-send -u critical "Battery is low ($PERCENT)%"
fi
@eze-kiel
Copy link
Author

eze-kiel commented Aug 6, 2021

Corresponding cronjob:

*/2 * * * * /path/to/batt_percentage.sh

This will execute the script every 2 minutes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment