#!/bin/bash # # Script for polling the battery state for systems where the battery # doesn't send acpi reports and has no alarm support. Unfortunately # laptop-mode-tools auto-hibernate module depends on that to work # properly. # # Script was found on gentoo-wiki.com with no license information. # Assuming it's public domain it is included here as such, WITHOUT ANY # WARRANTY. LIMIT="200" ## Hibernate if battery level drops below this (in mAh/mWh) SLEEP="60" ## Seconds between each battery level check BAT="BAT0" ## Part of path: /proc/acpi/battery/BAT HIBERNATE="/usr/sbin/pm-hibernate" ## command used to hibernate while [ true ]; do if [ -e "/proc/acpi/battery/$BAT/state" ]; then PRESENT=$(/bin/sed -ne "/present:/{s/^present:[ ]*\([a-z]*\)$/\1/p;q}" /proc/acpi/battery/$BAT/state) #echo $PRESENT if [ "$PRESENT" = "yes" ]; then STATE=$(/bin/sed -ne "/charging state:/{s/^charging state:[ ]*\([a-zA-Z]*\)$/\1/p;q}" /proc/acpi/battery/$BAT/state) BATTERY=$(/bin/sed -ne "/remaining capacity:/{s/^remaining capacity:[ ]*\([0-9]*\) m[WA]h$/\1/p;q}" /proc/acpi/battery/$BAT/state) #echo $BATTERY #echo $STATE if [ "$BATTERY" -lt "$LIMIT" ] && [ "$STATE" = "discharging" ]; then ## Comment out the following line if you don't ## want to log the event to system log: logger "Battery at ${BATTERY} mWh. Suspending the system to disk." #echo "Battery at ${BATTERY} mWh. Suspending the system to disk." ## Hibernate: exec "$HIBERNATE" fi fi fi sleep ${SLEEP}s done