#!/bin/bash ########################################################################### # Script for automatically shutting down an acpi-enabled laptop # # This script should be run periodically from cron or the like, and # can be modified to fit your needs. It checks the remaining battery # life and compare it to a threshold of the total capacity, and shuts # the computer down if the capacity is too low. This has been useful # when I have left my laptop running but unplugged and forgot about # it. This script should be run as root (for shutdown capability). # # I run this script every minute or so, so the crontab entry looks # like this: # * * * * * /usr/local/sbin/auto_lowbatt_shutdown # # Copyright (c) 2003 greg hamerly (greg_hamerly at baylor dot edu) # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA # 02111-1307, USA. or see http://www.gnu.org/copyleft/gpl.html # AC="/proc/acpi/ac_adapter/ACAD/state" BATT_STATE="/proc/acpi/battery/BAT1/state" BATT_INFO="/proc/acpi/battery/BAT1/info" # if the computer is plugged in, then just exit if grep -q "on-line" $AC then exit 0 fi # find out how much power is left, what is the total capacity, and the # threshold at which we will automatically shut down (2% of the # capacity) remain=`grep "remaining capacity:" $BATT_STATE | sed "s/[^0-9]//g"`; capacity=`grep "design capacity:" $BATT_INFO | sed "s/[^0-9]//g"`; let threshold="$capacity * 2 / 100" if [[ $remain -lt $threshold ]] then date=`date`; echo "shutting down due to low battery at $date" exec /sbin/shutdown -h now fi