Skip to content

Instantly share code, notes, and snippets.

@skulltech
Created January 10, 2021 05:49
Show Gist options
  • Save skulltech/61080afb765feb4d328cec86f0d14aa4 to your computer and use it in GitHub Desktop.
Save skulltech/61080afb765feb4d328cec86f0d14aa4 to your computer and use it in GitHub Desktop.
Bash script for maintaining optimal battery charging range on a laptop
#!/bin/bash
while true; do
export DISPLAY=:0.0
battery_level=$(acpi -b | grep -m1 -P -o '[0-9]+(?=%)' | awk '$1>0 { print $0; }')
ac_power=$(acpi -a | cut -d' ' -f3 | cut -d- -f1)
if [ "$ac_power" = "on" ]; then
if [ "$battery_level" -ge 80 ]; then
notify-send -u critical "Please unplug your AC adapter" "Battery level: ${battery_level}%" -i battery-full-charged
fi
else
if [ "$battery_level" -le 30 ]; then
notify-send -u critical "Please plug your AC adapter" "Battery level: ${battery_level}%" -i battery-caution
fi
fi
sleep 60
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment