# force shutdown before battery is empty



## ccc (Aug 1, 2009)

hi

I've freeBSD 7.2 with kde-lite installed on my eeepc 1000H.
Howto configute to force shutdown before battery is empty?


----------



## MG (Aug 1, 2009)

A script to run in the background:


```
#!/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
```


----------



## ccc (Aug 1, 2009)

THX, but howto install this script on freeBSd 7.2?


----------



## MG (Aug 1, 2009)

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


----------



## avilla@ (Aug 1, 2009)

MG said:
			
		

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



i suggest replacing

```
BATPWR=$(sysctl hw.acpi.battery.life. | cut -d ' ' -f 2)
```
with

```
BATPWR=$(sysctl -n hw.acpi.battery.life)
```


----------



## ccc (Aug 1, 2009)

MG said:
			
		

> just copy and run from shell as root
> 
> to completely put it on the background without a parent shell I always do:
> 
> ...



If I try as a root I'm getting the following:
	
	



```
# ./script.sh & disown
[1] 14556
disown: Command not found.
```


----------



## MG (Aug 2, 2009)

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.


----------



## ccc (Aug 2, 2009)

THX,

should I put this script for a cron job to check every 5 min?


----------



## MG (Aug 2, 2009)

No, it keeps on running until the battery is empty.


----------



## ccc (Aug 2, 2009)

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?


----------



## ccc (Aug 2, 2009)

```
#!/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
```


```
# chown root /usr/local/sbin/batterytest
# chmod u+x /usr/local/sbin/batterytest
```
add to crontab:
	
	



```
*/5 * * * * /usr/local/sbin/batterytest
```

greetings
ccc


----------



## DutchDaemon (Aug 2, 2009)

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.


----------



## DutchDaemon (Aug 2, 2009)

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.


```
#!/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
```


----------



## aragon (Aug 2, 2009)

DutchDaemon said:
			
		

> 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.


Never knew about @reboot.


----------

