notify-on-cricical-battery-level.sh 590 B

12345678910111213141516171819
  1. #!/usr/bin/env bash
  2. set -euo pipefail # bash strict mode
  3. ! [[ -f /usr/bin/acpi ]] && exit
  4. while :; do
  5. batt=$(acpi | tail -n1 | cut -d' ' -f4 | tr -d '%,')
  6. stat=$(acpi | tail -n1 | cut -d' ' -f3 | tr -d ',')
  7. if [[ $batt -lt 15 && $stat != "Charging" ]]; then
  8. notify-send -u critical "$stat" "Current battery level at ${batt}%"
  9. elif [[ $batt -gt 85 && $stat == "Charging" ]]; then
  10. notify-send -u critical "$stat" "Current battery level at ${batt}%"
  11. elif [[ $batt -lt 3 && $stat != "Charging" ]]; then
  12. systemctl poweroff
  13. fi
  14. sleep 5m
  15. done