DaemonForums  

Go Back   DaemonForums > OpenBSD > OpenBSD General

OpenBSD General Other questions regarding OpenBSD which do not fit in any of the categories below.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 26th March 2015
kmike kmike is offline
New User
 
Join Date: Mar 2015
Posts: 6
Default 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
Reply With Quote
  #2   (View Single Post)  
Old 28th March 2015
pawaan pawaan is offline
Fdisk Soldier
 
Join Date: Jan 2013
Posts: 82
Default

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
Reply With Quote
  #3   (View Single Post)  
Old 29th March 2015
bceverly bceverly is offline
Shell Scout
 
Join Date: Mar 2015
Posts: 88
Default

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.
Reply With Quote
  #4   (View Single Post)  
Old 29th March 2015
kmike kmike is offline
New User
 
Join Date: Mar 2015
Posts: 6
Default

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
The important stuff is in my low_batt script. If I boot and the battery is too low, I schedule a shutdown in 5 minutes anyway. What I do handle is that if the battery is low but is charging, then I don't schedule the shut down. As it stands now, if the battery gets low enough to trigger the rule, and I plug it into the wall, I have to go and kill the shutdown task to prevent the shutdown. And if I then take it off the charger before it gets above the threshold, it won't re-fire the rule again. I really should configure this script to run on both the charging sensor and the low charge sensor, though this requires changes to the script. It could then kill a pending shutdown, or schedule a shutdown on a charging->not charging transition. But even in its current condition it should give you some ideas on how to handle your situation.

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
Reply With Quote
  #5   (View Single Post)  
Old 30th March 2015
kmike kmike is offline
New User
 
Join Date: Mar 2015
Posts: 6
Default 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"
Script to handle the low-battery exception /usr/local/script/bat0_low:
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
And the script to handle the charging status change /usr/local/script/bat0_charging:
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.
Reply With Quote
  #6   (View Single Post)  
Old 30th March 2015
bceverly bceverly is offline
Shell Scout
 
Join Date: Mar 2015
Posts: 88
Default Thanks!

Appreciate the help.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

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


All times are GMT. The time now is 10:01 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick