#!/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
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
BATPWR=$(sysctl hw.acpi.battery.life. | cut -d ' ' -f 2)
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
# ./script.sh & disown
[1] 14556
disown: Command not found.
MG said:No, it keeps on running until the battery is empty.
#!/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
*/5 * * * * /usr/local/sbin/batterytest
#!/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
Never knew about @reboot.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.