force shutdown before battery is empty

hi

I've freeBSD 7.2 with kde-lite installed on my eeepc 1000H.
Howto configute to force shutdown before battery is empty?
 
A script to run in the background:

Code:
#!/bin/sh

LIMIT=3    # shutdown when less than 3% power
BATPWR=100

while [ "$BATPWR" -ge "$LIMIT" ]; do
  BATPWR=$(sysctl hw.acpi.battery.life. | cut -d ' ' -f 2)
  sleep 60  # wait one minute to next battery check
done

if [ $? == 0 ]; then 
  shutdown -h now
fi

exit 0
 
just copy and run from shell as root

to completely put it on the background without a parent shell I always do:

./script.sh & disown

Its better to test it in fake mode first by putting an echo before shutdown:e
 
MG said:
Code:
while [ "$BATPWR" -ge "$LIMIT" ]; do
  BATPWR=$(sysctl hw.acpi.battery.life. | cut -d ' ' -f 2)
  sleep 60  # wait one minute to next battery check
done

i suggest replacing
Code:
BATPWR=$(sysctl hw.acpi.battery.life. | cut -d ' ' -f 2)
with
Code:
BATPWR=$(sysctl -n hw.acpi.battery.life)
 
MG said:
just copy and run from shell as root

to completely put it on the background without a parent shell I always do:

./script.sh & disown

Its better to test it in fake mode first by putting an echo before shutdown:e

If I try as a root I'm getting the following:
Code:
# ./script.sh & disown
[1] 14556
disown: Command not found.
 
It's bash shell builtin. I forgot that. But ./scipt.sh & will do the job.

@xzhayon: You're right. I didn't know about the option to show only the value. That makes it a lot easier.
 
MG said:
No, it keeps on running until the battery is empty.

Then I should put to the startup scripts:

/etc/rc.d

or

/usr/local/etc/rc.d

instead of run it manually?
 
Code:
#!/bin/csh
# /usr/local/sbin/batterytest
# Baterry control
 
set restminutes=`/sbin/sysctl -n hw.acpi.battery.time`
set modus=`/sbin/sysctl -n hw.acpi.battery.state`
 
# does it have Power Supply connected?
if ( "${modus}" == "1" ) then
 
  # 7 Minutes?
  if ( "${restminutes}" < "7" ) then
    shutdown -p +2 "The Battery is empty! The Computer will shutdown after 2 Minutes!" &
  endif

endif
Code:
# chown root /usr/local/sbin/batterytest
# chmod u+x /usr/local/sbin/batterytest
add to crontab:
Code:
*/5 * * * * /usr/local/sbin/batterytest

greetings
ccc
 
Start it from /etc/rc.local, or from root's crontab (with @reboot as the time), if you want to keep it running in the background.
 
I found that I wrote a script for this some time ago ;)

It sends a warning email before shutting down,. so you can still plug the AC in and prevent the shutdown.

Code:
#!/bin/sh

while true

do

battery1=$( /sbin/sysctl -n hw.acpi.battery.life )

if [ $battery1 -le 10 ] && [ `/sbin/sysctl -n hw.acpi.acline`  == "0" ]
 then
  sleep 120
  battery2=$( /sbin/sysctl -n hw.acpi.battery.life  )
   if [ $battery2 -lt $battery1 ] && [ `/sbin/sysctl -n hw.acpi.acline` == "0" ]
    then
     echo "Insert power plug or kill PID $$ to prevent automatic shutdown." | mail -s "Battery $battery2 % - Will shutdown in 2 minutes" root
     sleep 120
      if [ `/sbin/sysctl -n hw.acpi.acline` == "0" ]
       then /sbin/shutdown -p now
      fi
   fi
fi

/bin/sleep 300

done
 
Back
Top