Last active
February 19, 2019 17:17
-
-
Save Frando/1becc932cc313d32df58cb42fc32399e to your computer and use it in GitHub Desktop.
Warn & hibernate on low battery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# put this in ~/.config/systemd/user/battery-warn.service | |
# and adjust the line in exec-start | |
[Unit] | |
Description=Battery warning | |
[Service] | |
Type=oneshot | |
Environment="DISPLAY=:0" "XAUTHORITY=/home/bit/.Xauthority" | |
ExecStart=/home/.../battery.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# put this in ~/.config/systemd/user/battery-warn.timer | |
# then enable the timer with | |
# systemctl --user enable battery-warn.timer | |
[Unit] | |
Description=Battery warning timer | |
[Timer] | |
OnCalendar=minutely | |
Persistent=true | |
Unit=battery-warn.service | |
[Install] | |
WantedBy=timers.target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# thresholds | |
notify="15" | |
warn="11" | |
shutoff="10" | |
# calculate charge | |
count=$(acpi -b | wc -l) | |
sum=$(acpi -b | egrep -o '[0-9]{1,3}%' | tr -d '%' | xargs -I% echo -n '+%') | |
charge=$(( sum / count )) | |
discharging() { | |
acpi -b | grep 'Discharging' > /dev/null | |
return $? | |
} | |
exitoncharge() { | |
if ! discharging; then exit 0; fi | |
} | |
countdown() { | |
notify-send "Battery low! Will shutdown in 30s." -u critical | |
sleep 10 | |
exitoncharge | |
notify-send "Battery low! Will shutdown in 20s." -u critical | |
sleep 10 | |
exitoncharge | |
notify-send "SHUTDOWN IN 10s" -u critical | |
sleep 5 | |
exitoncharge | |
notify-send "SHUTDOWN IN 5s" -u critical | |
sleep 5 | |
exitoncharge | |
systemctl hibernate | |
} | |
exitoncharge | |
if [ $charge -le $shutoff ]; then | |
countdown | |
elif [ $charge -le $warn ]; then | |
notify-send "Battery: $charge%" -u critical | |
elif [ $charge -le $notify ]; then | |
notify-send "Battery: $charge%" | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment