#!/bin/bash # batterycheck.sh # Checks your laptop battery state... # Warning for low capacity and shutdown the system when the battery is # totally discharged # # Version 0.1 # 05-Jan-2005 # # Copyright (C) 2005 substa # # # # # License: # # 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. # ############################################### # Change it according to your system settings # ############################################### STATE='/proc/acpi/battery/BAT1/state' SHUTDOWN=/sbin/halt LOGGER='/usr/bin/logger -t batterycheck ' OSDCTL=/usr/local/bin/osdctl BEEP=/usr/bin/beep LOW=5000 #first warning LOWER=2000 #second warning TOO_LOW=800 #shutdown the system ############################################### ACTUAL='NORMAL' while (true) do CHARGE=`grep -e charging $STATE | awk '{print $3}' -` CAPACITY=`grep -e remaining $STATE | awk '{print $3}' -` XCONTROL=`ps -a | grep xinit | awk '{print $4}'` if [ $CHARGE = 'charging' ] then if [ $ACTUAL != 'NORMAL' ] then ACTUAL='NORMAL' fi sleep 60s else if [ $CHARGE = 'discharging' ] then if [ $CAPACITY -lt $LOW ] && [ $ACTUAL == 'NORMAL' ] then ACTUAL='LOW' $LOGGER Battery discharging: low capacity if [ "$XCONTROL" = 'xinit' ]; then $BEEP -f 1000 -n -f 2000 -n -f 1500 $OSDCTL -s "Battery discharging, low capacity" else wall "Battery discharging: low capacity" fi fi if [ $CAPACITY -lt $LOWER ] && [ $ACTUAL != 'LOWER' ] then ACTUAL='LOWER' $LOGGER Battery discharging: very low capacity if [ "$XCONTROL" = 'xinit' ]; then $BEEP -f 1000 -n -f 2000 -n -f 1500 sleep 0.5s $BEEP -f 1000 -n -f 2000 -n -f 1500 $OSDCTL -s "Battery discharging, very low capacity, SAVE YOUR WORKS" else wall "Battery discharging: very low capacity, SAVE YOUR WORKS" fi fi if [ $CAPACITY -lt $TOO_LOW ] then $LOGGER Battery discharged: Shutting system down if [ "$XCONTROL" = 'xinit' ]; then $BEEP -f 1000 -n -f 500 -n -f 250 -n -f 120 $OSDCTL -s "Battery discharged, SHUTTING SYSTEM DOWN" else wall "Battery discarged: Shutting system down" fi $SHUTDOWN fi if [ $ACTUAL == 'NORMAL' ] then sleep 60s else if [ $ACTUAL == 'LOW' ] then sleep 30s else sleep 15s fi fi else $LOGGER Battery state not defined exit fi fi done