Skip to content

← Writing

engineering

Automated UPS Shutdown and Recovery with NUT

· Jerwin Arnado · 5 min read ·

A UPS that only holds the lights on for ten minutes isn’t really protecting a server — it’s just delaying the crash. What you actually want is a machine that notices the battery is running low, shuts itself down cleanly before the power cuts out, and then boots straight back up on its own once the wall power returns. No keyboard, no monitor, no you standing in the closet at 2am.

That whole loop is what Network UPS Tools (NUT) buys you. Here’s the setup I run on my Ubuntu box with a CyberPower UPS, start to finish.

1. The BIOS Prerequisite

Before touching any software, the motherboard has to be told to power on by itself when AC returns. Without this, NUT will shut the server down perfectly — and then leave it sitting dark until you press the button.

Enter your BIOS (Gigabyte, MSI, whatever you run), find Restore after AC Power Loss (sometimes labelled AC BACK), and set it to Power On / Always On. This is the hardware half of “recovery”; every other step below is the software half.

2. Install the Packages

Plug the UPS into the server over USB first, then install the NUT stack:

sudo apt update && sudo apt install nut nut-server nut-client

Optionally confirm the OS actually sees the UPS on USB before going further:

sudo nut-scanner -U

3. Describe the Hardware — ups.conf

This file tells NUT how to talk to your specific unit and, more importantly, sets the thresholds that decide when “low battery” means “shut down now.”

sudo nano /etc/nut/ups.conf

Add a section at the bottom:

[nutdev1]
    driver = "usbhid-ups"
    port = "auto"
    desc = "CyberPower UPS"
    ignorelb
    override.battery.charge.low = 20
    override.battery.runtime.low = 300

The [nutdev1] label is the name you’ll reference everywhere else. usbhid-ups is the driver for USB HID UPSes (most consumer CyberPower/APC units). The last three lines are the interesting part: ignorelb tells NUT to ignore the UPS’s own hardware “low battery” flag and trust your numbers instead — shut down at 20% charge or 300 seconds (5 minutes) of runtime remaining, whichever hits first. That headroom is what lets the OS finish flushing disks before the battery dies.

4. Set the Run Mode — nut.conf

Single machine, UPS attached locally, so NUT runs in standalone mode.

sudo nano /etc/nut/nut.conf

Set the mode line:

MODE=standalone

5. Wire Up the Monitor — upsmon.conf

This is the piece that watches the UPS and fires the actual shutdown when the thresholds from Step 3 trip.

sudo nano /etc/nut/upsmon.conf

Confirm the shutdown command is present and uncommented:

SHUTDOWNCMD "/sbin/shutdown -h +0"

Then add a monitor line at the bottom:

MONITOR nutdev1@localhost 1 admin password master

That reads as: watch nutdev1 on this host, it supplies 1 power source, authenticate as user admin with password, and this machine is the master (the one that runs the shutdown). Match admin/password to a user defined in /etc/nut/upsd.users.

6. Apply and Verify

Restart the services to load everything:

sudo systemctl restart nut-server nut-client

Then read live data straight off the UPS:

upsc nutdev1@localhost

You’re looking for a healthy battery.charge value and ups.status: OL (OL = online, running on wall power). If you see numbers, NUT is talking to the hardware.

7. The Test That Actually Matters

Everything above is theory until you cut the power. The point of this test is to watch the whole chain execute: the server shuts down, the UPS clicks itself off, and — when you plug it back in — the motherboard powers the whole thing back up.

Do this while the server is still on AC:

  1. Unplug the UPS from the wall. The server is now on battery.
  2. Force the shutdown sequence:
    sudo upsmon -c fsd
    

    fsd = forced shutdown, the same path a real dead battery would trigger.

  3. Wait it out. Let the server power off and let the UPS finish its countdown until it kills its own output. You want to confirm the UPS actually cuts power, not just that the OS halted.
  4. Plug the UPS back into the wall. Power returns, the UPS comes back, and thanks to Step 1 the motherboard boots the server automatically.

If the box comes back up on its own, the loop is closed: unattended shutdown on low battery, unattended recovery on power return. That’s the whole reason to run NUT instead of just staring at a battery icon.