|
OpenBSD General Other questions regarding OpenBSD which do not fit in any of the categories below. |
|
Thread Tools | Display Modes |
|
|||
Shutting down on low battery
What's the recommended way to force a laptop to shut down when the battery gets too low? I know the Gnome and KDE ports claim to have this functionality, but I'm not running a desktop manager.
I accidentally ran out of juice the other day and the laptop turned off without syncing. The filesystems were mostly ok, but I lost four of the nine cells in the battery. Since then I've put a rule in sensorsd.conf to run shutdown when the battery gets too low, but it seems like this might something that is handled automatically by the ACPI or APM subsystem. But I can't find any setting that might control this. I am seeing a low capacity sensor (hw.sensors.acpibat0.watthour2) which is currently showing 0.02 Ah, but that is not settable and I don't think the OS uses it for anything. The only thing google turned up was a blog post that described using a cron job to check the battery level, which sounds kludgier than just using sensord. Last edited by kmike; 27th March 2015 at 01:51 PM. Reason: misspelled sensorsd |
|
|||
I wish it were set as default like under NetBSD :
http://ftp.twaren.net/BSD/NetBSD/Net...sensor_battery Many times I had to manually fsck after losing work and it took an eternity |
|
|||
I made an entry in my sensorsd.conf but the problem is that within 60 seconds of boot, it fires the command so clearly I'm doing something wrong. Does anyone have a working /etc/sensorsd.conf that fires on low battery that I could please take a look at?
Thanks. |
|
|||
Edit: I have a new post below with updated rules and scripts that handle this more cleanly.
There really isn't much in the sensorsd file. Firing on boot is something you have to handle, because the rules are checked when the sensor changes, and at boot they change from uninitialized to their current value. You'll want to change the limits for your particular machine... Anyway, here's my /etc/sensorsd.conf.: Code:
# If the battery gets too low, shut down the machine in an orderly # fashion. When booted on mains the amphourX sensors are present, # when booted on battery the watthourX sensors are present. # low limit 9.2 Wh (20% of max) hw.sensors.acpibat0.watthour3:low=9.2Wh:command=/usr/local/script/low_batt %x %t %n %l %2 # low limit 0.93 Ah (20% of max) hw.sensors.acpibat0.amphour3:low=0.93Ah:command=/usr/local/script/low_batt %x %t %n %l %2 Here's my /usr/local/script/low_batt script: Code:
#! /bin/sh device=$1 # e.g. acpibat0 devtype=$2 # "watthour" or "amphour" sensornum=$3 # e.g. "3" exception=$4 # "below", "above", "within", "invalid", "uninitialized" curval=$5 dbglog=/tmp/low_batt.log date >> $dbglog echo device = $device >> $dbglog echo devtype = $devtype >> $dbglog echo sensornum = $sensornum >> $dbglog echo exception = $exception >> $dbglog echo curval = $curval >> $dbglog if [ "$device" = "acpibat0" -a "$sensornum" = "3" -a "$exception" = "below" ]; then sysctl hw.sensors.${device}.raw0 >> $dbglog charging_stat=`sysctl -n hw.sensors.${device}.raw0 | awk '{print $1}'` echo "Charging status code = $charging_stat" >> $dbglog if [ "$charging_stat" = "1" ]; then message="Low battery charge detected on $device $devtype $sensornum. The battery is currently discharging. Requesting shutdown since continued drainage risks damaging the battery." echo $message >> $dbglog logger "$message" /sbin/shutdown -ph +5 "$message" elif [ "$charging_stat" = "2" ]; then message="Low battery charge detected on $device $devtype $sensornum. The battery is currently charging. Will not shut down." echo "$message" >> $dbglog logger "$message" else message="Low battery charge detected on $device $devtype $sensornum. Unknown charging status code $charging_stat. Will not shut down." echo "$message" >> $dbglog logger "$message" fi fi echo . >> $dbglog Last edited by kmike; 30th March 2015 at 04:06 PM. Reason: refer to updated version |
|
|||
revised sensorsd and scripts
I've modified my sensorsd.conf and script files to handle the cases where the user fiddles with the power cable while the battery is low. This new version has rules on the two battery level sensors and the battery charging sensor (each with its own script).
/etc/sensorsd.conf Code:
# If the battery gets too low, shut down the machine in an orderly # fashion. When booted on mains the amphourX sensors are present, # when booted on battery the watthourX sensors are present. When the # battery charging status changes, make sure we are still doing the # right thing. # # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # IF YOU CHANGE THE LIMITS ON THE AMP/WATT RULES, REMEMBER TO CHANGE # THEM ON THE RAW0 ACTION AS WELL # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # low limit 9.2 Wh (20% of max) hw.sensors.acpibat0.watthour3:low=9.2Wh:command=/usr/local/script/bat0_low %x %t %n %2 %3 # low limit 0.93 Ah (20% of max) hw.sensors.acpibat0.amphour3:low=0.93Ah:command=/usr/local/script/bat0_low %x %t %n %2 %3 # When wall power goes off, we need to doublecheck the battery level. # When wall power goes on, we need to kill any pending shutdowns. hw.sensors.acpibat0.raw0:command=/usr/local/script/bat0_charging %x %t %n %2 "9.2" "0.93" Code:
#! /bin/sh # # Configuration # dbglog=/tmp/low_batt.log # production logger=logger shutdown=/sbin/shutdown # debugging #logger=false #shutdown=false # # Parse command line # device=$1 # e.g. acpibat0 devtype=$2 # "watthour" or "amphour" sensornum=$3 # e.g. "3" curval=$4 # floating point number curval_units=$5 #"Ah" or "Wh" lowlim=$6 # floating point number lowlim_units=$7 #"Ah" or "Wh" # calculate exception if (awk "BEGIN {exit(!($curval < $lowlim))}"); then exception='below' else exception='within' fi date >> $dbglog echo device = $device >> $dbglog echo devtype = $devtype >> $dbglog echo sensornum = $sensornum >> $dbglog echo exception = $exception >> $dbglog echo curval = $curval $curval_units >> $dbglog echo lowlim = $lowlim $lowlim_units >> $dbglog # # Handle the potential low-battery condition. # if [ "$device" = "acpibat0" -a "$sensornum" = "3" -a "$exception" = "below" ]; then sysctl hw.sensors.${device}.raw0 >> $dbglog charging_stat=`sysctl -n hw.sensors.${device}.raw0 | awk '{print $1}'` echo "Charging status code = $charging_stat" >> $dbglog if [ "$charging_stat" = "1" ]; then message="Low battery charge detected on $device $devtype $sensornum. The battery is currently discharging. Requesting shutdown since continued drainage risks damaging the battery." echo $message >> $dbglog $logger "$message" $shutdown -ph +5 "$message" elif [ "$charging_stat" = "2" ]; then message="Low battery charge detected on $device $devtype $sensornum. The battery is currently charging. Will not shut down." echo "$message" >> $dbglog $logger "$message" else message="Low battery charge detected on $device $devtype $sensornum. Unknown charging status code $charging_stat. Will not shut down." echo "$message" >> $dbglog $logger "$message" fi fi echo . >> $dbglog Code:
#! /bin/sh # # Configuration # dbglog=/tmp/batt_charging.log # production logger=logger # debugging #logger=false # # Parse command line # device=$1 # e.g. acpibat0 devtype=$2 # "watthour" or "amphour" sensornum=$3 # e.g. "3" curval=$4 # 1 = discharging, 2 = charging lowlim_watthr=$5 lowlim_amphr=$6 date >> $dbglog echo device = $device >> $dbglog echo devtype = $devtype >> $dbglog echo sensornum = $sensornum >> $dbglog echo curval = $curval >> $dbglog echo lowlim_watthr = $lowlim_watthr >> $dbglog echo lowlim_amphr = $lowlim_amphr >> $dbglog if [ "$curval" = "1" ]; then # # Discharging. # Run the low_batt script with the appropriate parameters. # watthours=`sysctl -n hw.sensors.acpibat0.watthour3 | awk '{print $1}'` amphours=`sysctl -n hw.sensors.acpibat0.amphour3 | awk '{print $1}'` if [ "$watthours" ]; then /usr/local/script/low_batt "acpibat0" "watthour" "3" "$watthours" "Wh" "$lowlim_watthr" "Wh" elif [ "$amphours" ]; then /usr/local/script/low_batt "acpibat0" "amphour" "3" "$amphours" "Ah" "$lowlim_amphr" "Ah" fi elif [ "$curval" = "2" ]; then # # Charging. We probably want to abort any shutdowns... # message="Stopping any shutdowns in progress that may have been issued by low_batt." echo $message >> $dbglog $logger "$message" kill -9 shutdown fi echo . >> $dbglog Last edited by kmike; 30th March 2015 at 04:40 PM. |
|
|||
Thanks!
Appreciate the help.
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Beep low battery | tixx8542 | OpenBSD General | 8 | 25th December 2014 01:10 PM |
dying battery always blinks .. is it a risk ? | daemonfowl | General Hardware | 11 | 25th July 2012 12:13 PM |
automatic or timed suspend on battery | asemisldkfj | OpenBSD General | 3 | 25th May 2011 11:55 PM |
Lost battery status in KDE4, FBSD 7.2-release | Mantazz | FreeBSD Ports and Packages | 0 | 3rd January 2010 02:00 AM |
Wireless networking xor battery monitor | Nobber | OpenBSD General | 5 | 27th February 2009 12:25 PM |