# Automatic shutdown on a specific battery percent



## Markand (May 10, 2010)

Hi,

I just noticed that even near 1% of battery, FreeBSD does not power down. I think it's the correct behavior expected but I would like shutdown at a percent like 2% or 1%.

I check powerd(8) but there wasn't feature like that. Do you have any hint?

Cheers.


----------



## aragon (May 10, 2010)

Have a look at devd(8).  Run it in debug mode and check if your system generates an ACPI event for a critical battery level.  If it does then you can make an entry in devd.conf(5) to handle that event.  If not you'll need to write a crontab or daemon that polls the battery level with acpiconf(8) or the hw.acpi.battery sysctl(8) variables.


----------



## DutchDaemon (May 10, 2010)

Yeah, it's quite easy to script. I used to run this in the background whenever I was on battery power, and it's warned/saved me quite a number of times when the AC had fallen out without me being aware.


```
#!/bin/sh

# who to warn
email=root
# battery level critical %
critlevel=10
# seconds to recheck and eventually act when battery is low
sleeps=120
# seconds to pause between script runs
loop=300

while true

do

# battery %
battery1=$( /sbin/sysctl -n hw.acpi.battery.life )
# AC plugged in?
acpower1=$( /sbin/sysctl -n hw.acpi.acline )

if [ ${battery1} -le ${critlevel} ] && [ ${acpower1} = "0" ]
 then
  /bin/sleep ${sleeps}

  battery2=$( /sbin/sysctl -n hw.acpi.battery.life  )
  acpower2=$( /sbin/sysctl -n hw.acpi.acline )

   if [ ${battery2} -lt ${battery1} ] && [ ${acpower2} = "0" ]
    then
     echo "Insert power plug or kill PID $$ to prevent automatic shutdown." | /usr/bin/mail -s "Battery ${battery2} % - Will shutdown in ${sleeps} seconds" ${email}
     /bin/sleep ${sleeps}

      acpower3=$( /sbin/sysctl -n hw.acpi.acline )

      if [ ${acpower3} = "0" ]
       then /sbin/shutdown -p now
      fi
   fi
fi

/bin/sleep ${loop}

done
```

A quick rundown of its 'flow': if battery level is below ten and AC power is not plugged in, wait 2 minutes, and try again. If the battery level is even lower and the AC power is still not plugged in, warn ${email} ("Plug in AC, or kill this script!"), and sleep two minutes. If the AC power is _still_ not plugged in by then, cleanly shutdown the system. Set levels, times, sleeps and email to whatever suits you best.


----------



## harishankar (May 11, 2010)

I'm sure the default power management of KDE or Gnome will handle this provided you're running on kdm or gdm. As long as ACPI is enabled and working properly it should work right thought I've not tried it myself on FreeBSD.

On my system the battery percent shows up all right so Gnome detects it no problem.


----------

