Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hibernate on Low Battery, when acpi does not report proper battery status
#!/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
}
@pvik

This comment has been minimized.

Copy link
Owner Author

commented Feb 26, 2018

add to (f)crontab to run every minute or couple of minutes

*/1   *   *   *   *   /opt/scripts/hibernate-on-low-battery.sh 
@pboesch

This comment has been minimized.

Copy link

commented Sep 14, 2018

Why don't you use upower?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.