Karl Godt wrote:I have modified it a bit first to check things :
Note : the PATH on my asus eee 1005 is slightly different :
/proc/acpi/button/lid/LID/state
Code: Select all
while true;do
LID_STATE=$(</proc/acpi/button/lid/LID/state)
if [ "$LID_STATE" != 'open' ];then
echo "$LID_STATE" >> /tmp/proc_acpi_lid_state.state
fi
sleep 2s
done
which gave me
state: open
state: open
state: open
state: open
state: closed
state: closed
state: closed
state: closed
state: closed
state: closed
state: closed
state: open
state: open
state: open
state: open
I have expanded the script a bit after finding that my Lucid machine died on me halfway through a movie when the battery ran out.
The script now also checks battery condition (/proc/acpi/battery/BAT0/state) and gives 2 alarms (configurable within the script thru variables 'low' and 'lowlow'). If battery reserve drops below shutdown level (also configurable thru variable 'shutdown') then a controlled shutdown is performed. I prefer this option over 'suspend-to-ram, as an unattended laptop in suspend-to-ram will eventually lose any unsaved data when the battery completely dies.
It uses 'aplay /usr/share/audio/beep_low.wav' and '/usr/share/audio/logout.wav' to give audible notifications, so if any of these are missing from the host system then it will probably hang. It may be necessary to comment them out
The remaining battery time for the warnings is calculated based on power consumption at that particular time, so the actual gap between the warnings may vary if the power consumption is varying.
I know most of this is available in acpi-laptop-tools, but my script has a small footprint and does all I need so far. It was also a nice scripting excercise.
It is listed below in case anyone feels they can use it
-------------------------------------------------------------------------------------------------------
#!/bin/sh
#===========================================================================#
# event handler for basic power management 2012 - d(O o O)b #
# #
# Automatic suspend-to-ram when laptop lid is closed #
# Configurable alarms for LOW and LOWLOW battery level #
# Controlled Shutdown at LOWLOWLOW #
# #
#===========================================================================#
#------------------------USER ADJUSTABLE VALUES--------------------------------
timeout=20 # Alarm dialog timeout - allows program to continue if
# dialog not accepted
low=10 # Low alarm setpoint (minutes)
lowlow=4 # LowLow alarm setpoint (minutes)
shutdown=2 # Controlled Shutdown setpoint (minutes) - 2 is OK for frugal
# may need longer for USB Savefiles to safely save
#------------------------------------------------------------------------------
let "low += 1" let "lowlow += 1" let "shutdown += 1"
flag1=0 flag2=0 #unset the low battery flags for initialization
while true #Endless loop.
do
if grep -q open /proc/acpi/button/lid/LID0/state
then
if grep -q discharging /proc/acpi/battery/BAT0/state
then
batteryLife=$(echo "scale=0;$(grep remaining /proc/acpi/battery/BAT0/state|awk '{print $3}') * 60 / $(grep rate /proc/acpi/battery/BAT0/state|awk '{print $3}')"| bc)
if (($batteryLife < $lowlow)) && (($flag2 == 0))
then
aplay /usr/share/audio/beep_low.wav /usr/share/audio/beep_low.wav /usr/share/audio/beep_low.wav
Xdialog --title "Very Low Battery Warning" --timeout $timeout --msgbox "$batteryLife minutes remaining \n Plug in or shut down immediately," 10 50
flag2=1 flag1=1 #prevents looped reporting
fi
if (($batteryLife < $low)) && (($flag1 == 0))
then
aplay /usr/share/audio/beep_low.wav /usr/share/audio/beep_low.wav
Xdialog --title "Low Battery Warning" --timeout $timeout --msgbox "$batteryLife minutes remaining \n Plug in soon." 10 50
flag1=1 #prevents looped reporting
fi
if (($batteryLife < 3))
then
Xdialog --title "Controlled Shutdown" --timeout 5 --msgbox "$batteryLife minutes remaining \n System going into controlled shutdown." 10 50
aplay /usr/share/audio/logout.wav
wmpoweroff
fi
else
flag1=0 flag2=0 #unset the low battery flags if we are on charge
fi
sleep 5 # minimise resource usage
else
aplay /usr/share/audio/beep_low.wav
echo mem > /sys/power/state # send the suspend signal if lid is closed
fi
done