My residence server is aware of when the ability goes out and shuts down earlier than catastrophe

It’s reassuring to have a battery backup on a home server. But it felt much less comforting once I realized my complete residence community would nonetheless go darkish with out warning as soon as the backup energy ran out.
This realization stored me watching the battery proportion and guessing when to show off the server. Luckily, the server could make the decision itself; I simply wanted to offer that job to a instrument referred to as Network UPS Tools (NUT).
It by no means will get a warning
NUT simply retains asking the UPS one query
Servers don’t know when the power is out, however NUT can change that. By default, NUT’s UPSMON checks the UPS standing uncovered by UPSD each 5 seconds. The driver individually polls the UPS {hardware}, so the precise detection delay will depend on each components of the chain.
Here is the report I obtained from working upsc command after unplugging the UPS from the wall on a charged battery:
|
Reading |
Before |
After |
|---|---|---|
|
ups.standing |
OL CHRG |
OB DISCHRG |
|
enter.voltage |
229.0 |
0.0 |
|
battery.voltage |
27.1 |
25.9 |
|
battery.cost |
97 |
97 |
I disconnected the UPS from mains energy at 21:17:38, and the journal logged “on battery” at 21:17:41. This three-second lag falls contained in the five-second polling interval. The battery cost does not replace as quickly because the standing and voltage readings, so it stayed at 97.
Three vital components make detecting energy outages and getting the knowledge to the server doable. A driver talks to the {hardware} (usbhid-ups, for my UPS), UPSD (NUT’s server course of) publishes that data, and UPSMON decides what to do with it. Merely being on battery alone is not going to set off a shutdown.
NUT additionally has a 15-second DEADTIME by default. If standing updates cease for longer than that, UPSMON marks the UPS useless. If it was final recognized to be on battery, NUT treats the lack of communication as a doable low-battery situation and will power a shutdown.
Low battery is an estimate
Doing the mathematics on how lengthy my server actually wants
Combined, my server, swap, and entry level drew 62W, about 7% of my UPS capability. Using the producers’ runtime figures, which cowl half- and full-load circumstances and exceed my 62W load, I couldn’t estimate how long my UPS would last. So I needed to measure.
I used a 62W dummy load to run two full discharges, stopping on the battery cutoff to keep away from deep discharge. At the beginning, the UPS estimated 88 and 87 minutes, however the battery lasted 66 minutes and 5 seconds and 64 minutes and 40 seconds. In each runs, the preliminary estimate was a couple of third increased than the runtime I measured.
The UPS raised its low-battery flag at 10% cost, though its reported runtime was nonetheless 520 to 540 seconds, above my 300-second threshold. That suggests the cost threshold might have triggered the flag, though the readings alone do not show which situation brought on it. In my two exams, the flag appeared 305 to 330 seconds earlier than cutoff.
Shutdown usually takes between 31 and 37 seconds. Budgeting 5 seconds for detection lag, the worst case is about 42 seconds. So the flag nonetheless leaves about seven occasions the time required for a protected shutdown.
|
Low-battery set off |
Timer on battery |
|
|---|---|---|
|
Starts when |
The UPS experiences low battery |
The server has been on battery for a set time |
|
Good at |
Ignoring brief blips |
Not relying on the UPS’s estimate |
|
Watch out for |
A threshold that leaves too little time |
Must be canceled if energy returns |
My configuration begins a 10-minute countdown when the UPS switches to battery energy and cancels it if mains energy returns earlier than the timer expires. However, it begins the server shutdown if the countdown reaches zero first.
Under my check load, the UPS lasted about 66 minutes. This means the server shuts down in roughly 10 minutes and 42 seconds into an outage, leaving about 55 minutes of the measured runtime unused. This is extra of an estimate from my check than a assure for an actual outage. Some UPS fashions permit low-battery threshold adjustment, relying on the UPS and driver.
The ten-second clock
What a clear shutdown protects {that a} sudden minimize doesn’t
After an influence minimize, a server might boot usually and nonetheless have broken information. Ext4 and different journaling file programs can get better their very own inside information, however they cannot end a database transaction or save a file interrupted mid-write. A clear shutdown provides purposes time to complete pending work and shut recordsdata earlier than the ability disappears.
By default, Docker waits 10 seconds after sending a container’s cease sign earlier than killing it. I used a small home made container that ignores SIGTERM (the sign that asks a course of to cease) to check it. The check container ran the entire 10 seconds earlier than exiting with exit code 137.
I checked my PostgreSQL 16 container and located the stop_grace_period was set to 60s. Running time docker cease postgres took 2.1 seconds with the database idle and 9.4 seconds whereas it carried out a bulk import. That second worth was too near the ten-second timeout, which can be of venture on a busier database.
In my setup, containers cease concurrently, so the slowest one units the size. So, whereas NUT begins the shutdown, I’ve no proof that every one purposes completed safely.
Pulling the plug on function
It took 32 seconds from command to poweroff once I ran upsmon -c fsd at 100% cost, however this solely proves a shutdown path. I additionally needed to see whether or not NUT would detect an actual outage. So at 97% cost, I unplugged my UPS after briefly setting the timer to 2 minutes.
Test timeline:
|
Time |
Event |
|---|---|
|
21:17:38 |
UPS unplugged |
|
21:17:41 |
On battery detected |
|
21:19:41 |
Timer fires, shutdown begins |
|
21:20:12 |
Power off accomplished (31s after shutdown began) |
|
21:21:10 |
Mains restored |
|
~21:22:30 |
Server again up |
The check additionally revealed that my ISP router was plugged right into a wall socket. Unplugging dropped the web whereas the house server stayed working.
NUT can’t predict an influence outage, however my server can now discover one, resolve when to close down, and depart room for its purposes to cease. That’s greater than an everyday battery backup provides you.
