Last active
September 14, 2018 21:21
-
-
Save pvik/b45b16248206b31119cfad2ba7f0b0ea to your computer and use it in GitHub Desktop.
Hibernate on Low Battery, when acpi does not report proper battery status
This file contains hidden or 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 | |
# Battery threshold below which, script will hibernate if discharging | |
batt_threshold=20 | |
# temporary file to store the previous battery charge value, to determine if battery is being discharged | |
batt_charge_file="/tmp/batt_charge" | |
# read in batt charge from acpi | |
echo "`acpi -b`, `cat /tmp/batt_charge`%" | awk -F'[:,%]' '{print $2, $3, $5, ($5 - $3), ($3-$5)<0?"discharging":"powered"}' | { | |
read -r acpi_status capacity old_capacity capacity_diff status | |
logger "$0: ($acpi_status) $status $capacity <= $old_capacity" | |
# if ( (capacity - old_capacity) < 0 && capacity < 15 ) | |
if [ "$status" = discharging ]; then | |
#notification 1% before laptop is going to be hibernated | |
if [ "$capacity" -lt "$(($batt_threshold + 2))" ]; then | |
notify-send "Battery Low" "System will be hibernated soon..." -u CRITICAL | |
fi | |
if [ "$capacity" -lt "$batt_threshold" ]; then | |
logger "$0: Critical battery threshold" | |
systemctl hibernate | |
fi | |
fi | |
echo $capacity > $batt_charge_file | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
add to (f)crontab to run every minute or couple of minutes