Skip to content

Instantly share code, notes, and snippets.

@joshmfrankel
Last active August 29, 2015 14:16
Show Gist options
  • Save joshmfrankel/c6b0c0bb69c987fd8395 to your computer and use it in GitHub Desktop.
Save joshmfrankel/c6b0c0bb69c987fd8395 to your computer and use it in GitHub Desktop.
SHELL: Battery script to make sure overcharging doesn't happen
#http://askubuntu.com/questions/518928/how-to-write-a-script-to-listen-to-battery-status-and-alert-me-when-its-above
#!/bin/bash
notify-send "Battery monitoring enabled"
while true
do
export DISPLAY=:0.0
battery_level=`acpi -b | grep -P -o '[0-9]+(?=%)'`
if on_ac_power; then
if [ $battery_level -ge 90 ]; then
notify-send "Battery charging above 90%. Please unplug your AC adapter!" "Charging: ${battery_level}% "
sleep 20
if on_ac_power; then
gnome-screensaver-command -l ## lock the screen if you don't unplug AC adapter after 20 seconds
fi
fi
else
if [ $battery_level -le 15 ]; then
notify-send "Battery is lower 15%. Need to charging! Please plug your AC adapter." "Charging: ${battery_level}%"
sleep 20
if ! on_ac_power; then
gnome-screensaver-command -l ## lock the screen if you don't plug AC adapter after 20 seconds
fi
fi
fi
sleep 300 # 300 seconds or 5 minutes
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment