This discussion is specifically tailored for dumb UPS control.
However, most of the process is about the same for dumb UPSs and smart
UPSs. The biggest difference is in the details of how the UPS
monitoring daemon (typically powerd) communicates with the UPS.
Before doing anything, I suggest the following algorithm:
powerd (or some sort of equivalent) on the computer.init to do something reasonable on powerfail and
powerok events (like start a shutdown and kill any currently
running shutdowns, respectively, for example).
When the power goes out, the UPS continues to power the computer and signals that the power went out by throwing a relay or turning on an opticoupler on it's control port.
The cable is designed so that when the UPS throws said relay,
this causes a particular serial port control line (typically
DCD) to go high.
The powerd daemon monitors the serial port. Keeps raised/lowered
whatever serial port control lines the UPS needs to have
raised/lowered (typically, DTR must be kept high and whatever line
shuts off the UPS must be kept low). When powerd sees the
UPS control
line go high, it writes FAIL to /etc/powerstatus and
sends the
init process a SIGPWR signal. (Older versions of
powerd and initd wrote to /etc/powerfail.)
When the control line goes low again, it writes OK to
/etc/powerstatus and sends init
a SIGPWR signal.
When it receives a SIGPWR, it looks at /etc/powerstatus.
If it contains FAIL it runs the powerfail entry from
/etc/inittab. If it contains OK it runs the
powerokwait entry from inittab.
The following presupposes that you have a cable that works properly
with powerd. If you're not sure that your cable works (or how it
works), see section
Reverse-engineering cables and hacking powerd.c
for information on dealing with poorly described cables and
reconfiguring powerd.c. Sections
Serial port pin assignments and
Ioctl to RS232 correspondence
will also be useful.
If you need to make a cable, see section How to make a cable for the overall details, and the subsection of section Info on selected UPSs that refers to your UPS. The latter might also include information on manufacturer supplied cables. You may want to at least skim all of section Info on selected UPSs because each section has a few additional generally helpful details.
/etc/inittab. Put in something like this:
# What to do when power fails (Halt system & drain battery :): pf::powerfail:/etc/powerfailscript +5 # If power is back before shutdown, cancel the running shutdown. pg:0123456:powerokwait:/etc/powerokscript
/etc/powerfailscript and
/etc/powerokscript to shutdown in 5 minutes (or whatever's
appropriate) and kill any existing shutdown, respectively.
Depending on the version of shutdown that you're using, this will
be either so trivial that you'll dispense with the scripts, or be a
1 line bash script, something along the lines of:
kill `ps -aux | grep "shutdown" | grep -v grep | awk '{print $2}'`
and you'll keep the scripts. (In case it doesn't come out right,
the first single quote on the above line is a backquote, the
second and third are single quotes, and the last is also a
backquote.)
init to re-process the inittab file with the command:
telinit q
powerd gets run upon startup. The syntax
is:
powerd <line>
Replace <line> with the serial port that the UPS
is connected, such as /dev/cua1.
powerd.
/etc/powerfailscript runs.shutdown is running./etc/powerokscript runs./etc/powerfailscript is not running.shutdown is no longer running.shutdown is running
and wait. Make sure that the computer shuts down cleanly before the
battery on the UPS gives out. This is dangerous because if the
power goes out before the computer shuts down, you can end up with a
corrupt file system, and maybe even lose all your files.
You'll probably want to do a full backup before this test, and
set the shutdown time extremely short to begin with.Congratulations! You now have a Linux computer that's protected by a UPS and will shutdown cleanly when the power goes out!
powerd.c to monitor the line indicating that the batteries
are low. When the batteries get low, do an immediate shutdown.powerfail situation, then it turns off the UPS after doing
everything necessary.