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

linuxpower-managementshutdownUbuntuups

I'm wondering that I could not find anything useful on Google.

I'm using a cheap UPS which actually does keep my computer powered for awhile if there are some electricity issues (they are very common in all South-Eastern Asia).

But what if I, say, leave the machine turned on and go out of my house to buy something in a shop? Or I want to download a game or any other big thing, and I'm leaving the machine turned on in the night time?

There must be a way to turn the computer off 'softly' if there's no electricity.

I'm using Ubuntu and it would be just sufficient to execute 'shutdown now'.

I can't believe it's impossible because I think my requirements are quite common.

How can I accomplish this?

Best Answer

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.

Related Question