#!/bin/bash # written by Peter Selinger, modified 2004/01/23 # This script is called by acpid whenever there is an ACPI event, as # configured in /etc/acpi/events/acpid.conf. The way I have set things # up is that this script does all the checking for what type of event # has occurred. # remember parameters EVENT="$1" # configuration BEEPER=/etc/acpi/actions/beeper.sh WARN_LEVEL=5 SHUTDOWN_LEVEL=2 case "$EVENT" in button/power* | button/lid* ) # handle power button or lid event # ignore for now ;; ac_adapter* | battery* ) # A battery or power event has occurred. # Check if we are running out of battery, and do something about it. # first check if we are on AC power. if acpi -a | grep -q on-line; then # if yes, everything is fine. echo "$0: On AC power." [ -x $BEEPER ] && $BEEPER off exit 0 fi # determine the highest level of any connected battery. levels=`acpi -b | sed -n -e 's/.*[^0-9]\([0-9][0-9]*\)%.*/\1/p'` # if no battery levels were found, we are in trouble if [ -z "$levels" ]; then echo "$0: warning: not on AC power, and no batteries found!" exit 1 fi # else find the highest level of any battery high=0 for i in $levels; do if [ "$i" -gt "$high" ]; then high="$i" fi done echo "$0: On battery. Levels: $levels. Highest: $high" # if the level is $WARN_LEVEL or less, give a warning to the user, # and start beeping. If the level is $SHUTDOWN_LEVEL or less, # initiate shutdown if [ "$high" -le "$SHUTDOWN_LEVEL" ]; then [ -x $BEEPER ] && $BEEPER on echo "$0: Initiating shutdown" /sbin/shutdown -h now "Initiating shutdown because the battery charge has dropped to $high%." & elif [ "$high" -le "$WARN_LEVEL" ]; then [ -x $BEEPER ] && $BEEPER on wall "Warning: low battery ($high%). System will shut down at $SHUTDOWN_LEVEL%." else # battery level is high; stop beeper if any [ -x $BEEPER ] && $BEEPER off fi ;; esac