Skip to content

Instantly share code, notes, and snippets.

@manparvesh
Last active August 25, 2019 13:32
Show Gist options
  • Save manparvesh/7aefc55f8b705423de7cabe6384c08e9 to your computer and use it in GitHub Desktop.
Save manparvesh/7aefc55f8b705423de7cabe6384c08e9 to your computer and use it in GitHub Desktop.
Shell file to notify when your laptop battery life is less than 20% or more than 90% as is charging
#!/bin/bash
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}% "
spd-say "charge full, fulll, full"
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 20 ]; then
notify-send "Battery is lower than 20%. Need to charg! Please plug your AC adapter." "Charging: ${battery_level}%"
spd-say "battery low, low, low"
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 120 # 120 seconds or 2 minutes
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment