I recently purchased an APC Back-UPS 600 for my home server (the one that runs this blog!) so I could make it shut down safely once it's on battery power. I did some research on some Linux software that could handle the communication between the UPS and server, and I found apcupsd, a daemon that polls the UPS status every once in a while and reacts accordingly. Perfect!
But while the documentation is very good and extensive, I found it hard to discover what I needed to do – run a custom script when the UPS is on battery power. This seems like a common task one would like to do, to e.g. perform cleanup or notify someone when the power's out.
TL;DR: Put your scripts in
/etc/apcupsd that are named the appropriate event you want to react to.
For example, if you wanted to react to the
onbattery event (fired when APCUPSD is sure that the UPS is on battery power), you would make a script in
/etc/apcupsd/onbattery with these contents:
#!/bin/bash # Do whatever you need here! curl "https://example.org/powerfailure"
Make sure to
chmod +x your script, too. For a full list of events, check here.
For my use case, I made the script send a message to my Telegram channel when the UPS loses power:
...and another when power is restored:
I hope this saved you a few Google searches!