#!/bin/bash set -e trap 'xmessage -nearmouse "WARNING: battery monitor died!"' EXIT was_notified=0 while true; do acpi_data=$(acpi -b) status=$(echo -n "$acpi_data" | awk -F'[,:%]' '{print $2}') capacity=$(echo -n "$acpi_data" | awk -F'[,:%]' '{print $3}') echo "Status: $status; Capacity: $capacity; was_notified: $was_notified" if [[ "$status" =~ "Charging" ]]; then was_notified=0 fi if [[ "$status" =~ "Discharging" && "$capacity" -lt 25 && "$was_notified" = 0 ]]; then xmessage -nearmouse "$acpi_data"& was_notified=1 fi if [[ "$status" =~ "Discharging" && "$capacity" -lt 15 ]]; then bash -c 'echo "Battery critical, suspending"; acpi -b' | xmessage -nearmouse -file -& sleep 5 systemctl hibernate fi sleep 10 done