Having a spare Asus EEE 701 netbook, I've converted it as a home server for music streaming, DVB-T streaming, web server, ...
This netbook is consuming very little power and is running under Debian in console mode without any graphical interface.
As every netbook, the EEE 701 is having an inbuilt battery, which can be used a very KISS (Keep It Simple and Stupid) Uninterrupted Power Supply system.
With proper scripts, it is possible to detect power failure and to follow the battery level & to initiate a clean shutdown process when battery level is too low after a long power failure.
This article will explain how to :
- detect power failure
- check battery level
- start a shutdown process when battery level is reaching predefined low level
With that, your server will shutdown properly whatever happens with its power supply.
1. Install ACPI tools
First thing to do is to install the acpi command line utility, which will provide the needed tools.
# aptitude install acpi
You can now get the basic informations about the current power supply condition of your server.
Here are the informations when power is plugged & battery is fully charged
# acpi -V
Battery 0: Full, 100%, rate information unavailable
Battery 0: design capacity 5200 mAh, last full capacity 5200 mAh = 100%
Adapter 0: on-line
Thermal 0: ok, 63.0 degrees C
Thermal 0: trip point 0 switches to mode critical at temperature 90.0 degrees C
Cooling 0: Processor 0 of 7
Here are the informations when power is unplugged & battery still charged at 80%
# acpi -V
Battery 0: Discharging, 80%, rate information unavailable
Battery 0: design capacity 5200 mAh, last full capacity 5200 mAh = 100%
Adapter 0: off-line
Thermal 0: ok, 63.0 degrees C
Thermal 0: trip point 0 switches to mode critical at temperature 90.0 degrees C
Cooling 0: Processor 0 of 7
2. Supervision script
The main goal of the supervision script is to check periodically the power supply and the battery level condition.
If power is down and battery level is too low, it initiates a server shutdown.
/root/shutdown-on-lowbattery
#!/bin/bash
#
# Check every minute for shutdown in case of power failure and low battery level
#
# set battery level limit before shutdown
BATTERYLIMIT=20
# permanent loop
while [ 1 ]
do
# wait 1 minute till the next check
sleep 1m
# get the battery status line indicating the discharging condition
BATTERYLOAD=`acpi -V | grep Battery | grep Discharging | sed 's/^.*, \([0-9]*\)%,.*$/\1/g'`
# if battery is in discharging state
if [ -n "$BATTERYLOAD" ]
then
# if the battery load status is below the limit
if [ "$BATTERYLOAD" -lt "$BATTERYLIMIT" ]
then
# log shutdown process
logger Shutdown On Low Battery - Shutdown started
# shutdown the server
shutdown -h now
fi
fi
done
Do not forget to make the script executable.
# chmod +x /root/shutdown-on-lowbattery
3. Script automatic startup
When the battery supervison script is operational, you need to declare it so that it is started during boot-up time.
We will do it thru a init.d script.
/etc/init.d/shutdown-on-lowbattery
#!/bin/bash
#
### BEGIN INIT INFO
# Provides: shutdown-on-lowbattery
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Required-Start:
# Required-Stop:
# Short-Description: Check every minute for shutdown
# Description: Check every minute for shutdown in case of power failure and low battery level
### END INIT INFO
#
# main()
#
# package acpi needs to be installed
# set the minimum battery power % before shutdown process
NAME=shutdown-on-lowbattery
DAEMON=/root/$NAME
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=shutdown-on-lowbattery
#
case "${1:-''}" in
'start')
# log the daemon start
logger Shutdown On Low Battery - Daemon started, will check battery status every minute
# start the supervision daemon
start-stop-daemon --start --background --make-pidfile --pidfile $PIDFILE --exec $DAEMON
;;
'stop')
# log the daemon stop
logger Shutdown On Low Battery - Daemon stopped
# kill the process
start-stop-daemon --stop --pidfile $PIDFILE --quiet
rm $PIDFILE
;;
'restart')
;;
'reload'|'force-reload')
;;
'status')
;;
*)
echo "Usage: $SELF start|stop"
exit 1
;;
esac
You then need to make that script executable and to make it start automatically at boot time.
To do so, start a console, declare the script thru update-rc.d and reboot your server for the script to become operationnal.
# chmod +x /etc/init.d/shutdown-on-lowbattery
# update-rc.d shutdown-on-lowbattery defaults 90 10
# reboot
Your home server is now protected against power failure.
In case of long time failure, it will shutdown gracefully, waiting for you to restart it.
Hope it helps.