46 lines
1.1 KiB
Bash
Executable file
46 lines
1.1 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
#if [ -n "$(pgrep batteryobserver | grep -v $$)" ]; then
|
|
# echo "Battery Observer already running"
|
|
# exit 0
|
|
#fi
|
|
|
|
#Config
|
|
WARNING_LEVEL=10
|
|
SHUTDOWN_LEVEL=2
|
|
#End config
|
|
|
|
|
|
STATE=0
|
|
|
|
while true; do
|
|
level=$(acpi -b | awk -F, 'BEGIN{s=0}{s+=$2}END{print int(s/NR)}')
|
|
charger_connected=$(acpi -b | grep Charging)
|
|
case "$STATE" in
|
|
0) # battery ok
|
|
if [ "$level" -le "$WARNING_LEVEL" -a -z "$charger_connected" ]; then
|
|
zenity --warning --text="Your battery is getting low\!" --no-wrap &
|
|
STATE=1
|
|
fi
|
|
;;
|
|
1) # battery low
|
|
if [ -n "$charger_connected" ]; then
|
|
STATE=0
|
|
elif [ "$level" -gt "$WARNING_LEVEL" ]; then
|
|
STATE=0
|
|
elif [ "$level" -le "$SHUTDOWN_LEVEL" ]; then
|
|
systemctl hibernate;
|
|
else
|
|
notify-send -u critical -a "BatterySystem" "Battery is low!" "Please connect a charger"
|
|
fi
|
|
;;
|
|
esac
|
|
if [ $level -gt 50 ]; then
|
|
sleep 2m
|
|
elif [ $level -gt 30 ]; then
|
|
sleep 1m
|
|
else
|
|
sleep 5
|
|
fi
|
|
done
|
|
|