Skip to content

Instantly share code, notes, and snippets.

@pvik
Last active September 14, 2018 21:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pvik/b45b16248206b31119cfad2ba7f0b0ea to your computer and use it in GitHub Desktop.
Save pvik/b45b16248206b31119cfad2ba7f0b0ea to your computer and use it in GitHub Desktop.
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
Copy link
Author

pvik 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
Copy link

pboesch 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