Have UPS shut down computer automatically in Linux when the power goes out

Most UPSs (probably all but the cheapest), come with this capability. Otherwise, the UPS would have little benefit when you aren't there. If yours doesn't have it, the safe practice would be to not leave the computer on (in other than hibernation mode), when it's unattended.

Data Connection

If the UPS is doing its job, the computer doesn't know that the utility power went out. The only way for the computer to know to shut down, while it can still do so gracefully, is if the UPS can communicate the situation. This feature requires a data connection between the UPS and the computer.

At the computer end, this is typically a USB connection if the UPS is intended for a single personal computer. The port on the UPS, itself, can vary. On many, it looks like a telephone jack (not to be confused with an actual telephone jack on some models for surge protection on the phone line). The UPS comes with whatever cable is needed for the data connection.

In more advanced models, the UPS also uses this connection to communicate status information, like battery condition.

Software

You need software on the computer to make use of this information. The UPS typically comes bundled with software for Windows. Linux often requires an extra step or two. A few distros come bundled with either a generic program or one from a particular UPS manufacturer (which would likely work only with that manufacturer's products).

If your distro doesn't, some of the UPS manufacturers have a Linux version of their software available for download on their web site. So, if you want a particular brand of UPS, that's the first place to check. One weakness of Linux is that driver software is more limited in availability, and implementation is more "hands-on", than in Windows. Some hardware purchasing decisions need to be driven by the availability of Linux support.

If you want a particular brand of UPS and they don't offer Linux software, these are the most common options:

  • apcupsd

    APC is sort of a special case. They're one of the largest UPS manufacturers, with an extensive range of models. They've invested in supporting and certifying their models in Linux.

    Their free apcupsd software is bundled with a few distros and is generally available in other distros' repositories, easily selected and installed via your package manager. Although I've never used it, it is generally well-regarded and appears to be the current de facto standard.

    One limitation is that it only supports APC products. However, if you don't have a strong reason to buy a brand that lacks Linux software, picking from APC's offerings is an easy option.

  • NUT

    If you absolutely must have a UPS from a manufacturer that doesn't offer Linux software, you will likely be able to use Network UPS Tools (NUT). This is a free application that handles control, monitoring, and management of almost anything power-related. It's probably overkill for a UPS on one computer, and it requires some configuration (not the best solution for newbies). This is generally available in most distros' repositories.

    If you are contemplating buying a UPS for which your only software option is generic software like NUT, I urge you to read the manual first to satisfy yourself that you are up to the task of configuring it.

  • WinPower

    Another generic program that runs in Linux (and other OSes), is WinPower. I have no personal familiarity with this program, other than skimming through the manual. I'm guessing that it isn't widely used because the only mentions I could find on SU were two unanswered questions from people who couldn't get it to detect the UPS (there are also only a few questions about NUT, but these are mostly about using the available data).

    Like NUT, WinPower also requires a detailed configuration step, and you will need the technical manual for your UPS for the information. As with NUT, if you plan to rely on WinPower, I urge you to read the manual first to confirm that you have access to the needed technical information, and satisfy yourself that you are up to the task of configuring it, before buying a UPS that lacks Linux software.


I'm using apcups with a CyberPower CP1000AVRLCDa that I just bought, and its working great with my Linux Mint machines.

You can even set it up in a situation with more than one computer on the same UPS, one is the master and connects to the UPS via the USB cable, the others talk to the apcupsd on that master and should be configured to shut down before the master does.

However, directly to your question - you need a way to detect power outage. If the UPS doesn't supply it (via some sort of 'relay' (switch), or via ethernet, or serial, or USB), then you'll need another way to detect loss of power so you can shut down.

I suppose a silly way to detect loss of power would be to have another computer that isn't connected to the UPS whose only purpose is to quit talking to the 'protected' computer when it loses power, so that the protected computer will know that power just went off and shut itself down. (Did that make any sense?)

A better way would be some sort of input to your PC that changes based upon presence of AC power in the room. Besides another computer whose only purpose is to die upon loss of power, you could perhaps set up a device that sets a bit on the parallel port to one value when power is present and to the opposite value when the power goes out. I can think of many ways to do this, from a box with an LED powered by the mains which illuminates a photodiode or similar which then pulls the parallel port bit the appropriate way, to a latching relay that is powered by the mains, and that uses the free contacts to pull your parallel port bit appropriately. Before you turn your computer on, latch the relay on first so that your computer sees the power as good. Then, when power goes off the relay drops out and stays off, telling your computer to shut down.

On the other hand, if your power might randomly bounce for a few seconds and then come back so you don't want a single power loss event to kill your pc, then don't use a latching relay - use a 'regular' relay connected to the mains and thus provide a real-time indication of the power state. On the pc, watch that line and if it goes 'no power' for some length of time, shut down.

Note - the above is a somewhat short set of ideas of how to detect power loss on your computer so you can make it shut down. I'm assuming some level of electronic understanding here... And be VERY CAREFUL with anything directly connected to the power mains!