Posted on ::

I released ups-monitor.sh, a bash script that decides what happens when the power goes out.

My UPS powers my servers, NAS, main switch and wifi access point. I want to cut off first my workstations in a clean way, to keep as much battery power to provide my main file server access via wifi for my phones and tablets.

Configuring NUT scripts for this seemed verbose and complex for my small setup, so I basically coded a simple but flexible bash control layer over the NUT infrastructure

The staged shutdown🔗

So the script shuts things down in stages, as the battery drains:

  • 90% battery: shut down the non-essential "first" servers — storage, backups, the toy boxes.
  • 80% battery: shut down the "extra" servers.
  • 20% battery: shut down the monitoring host itself, and put the UPS in standby.

The UPS on the final step powers everything off and goes to sleep; when the mains come back, it wakes up, powers the equipment, and the machines can boot again. Recovery is automatic.

The staging is fully configurable: the thresholds and the server lists live in /etc/ups-monitor.conf, and servers can be prefixed h: to hibernate instead of shutdown, or wh:/ws: for Windows hosts.

The config is bash🔗

As I often set up my scripts, the config file is sourced by the script, so it is not a list of options in some syntax, it is pure bash. You can override any variable — and any function. Redefine info to text you instead of mailing you. Redefine remoteshut to do whatever exotic thing your network needs. You can even write "pseudo servers" as funcname:params in the server lists, and the script will run that function before shutting down — say, dump the databases before the machine dies. The anti-framework approach, same as my colas-bash-lib collection.

It just polls the UPS every five seconds, so bash provides simplicity, stability and flexibility with a negligible overhead.

Goodies🔗

  • A dry-run mode toggled by touch /tmp/NOUPSMON — no restart needed — so you can unplug the UPS and watch the logs before trusting it for real.
  • Email alerts for power lost and restored, overload, and battery-to-replace. AVR events (TRIM/BOOST) logged with the exact input voltage, for info. Logs rotated monthly, and a clean command for the cron.

Compatible UPS🔗

One advice: choose a pure sine wave UPS, otherwise some active-PFC power supplies may panic and shut down abruptly when the UPS switches to batteries. Save yourself the uncertainty.

Installation, Usage, and more info🔗

github.com/ColasNahaboo/ups-monitor.sh

Note: This is AI-co-advised: chatting with Gemini helped me as a UPS novice, but it did not write the code or the docs.

Table of Contents