How Linux Interacts with a UPS
The Linux operating system interacts with an Uninterruptible Power Supply (UPS) through a combination of kernel-level hardware drivers, user-space daemon services, and automated event-handling scripts. When connected via USB, serial, or a network interface, Linux detects the UPS, continuously monitors metrics like battery health and line voltage, and executes preconfigured actions—such as alerting administrators or initiating a safe system shutdown—when power fails.
Hardware Interfaces and Kernel Detection
Linux communicates with a UPS through three primary physical interfaces:
- USB: Most modern desktop and rackmount units
utilize USB. The Linux kernel uses the
usbhidsubsystem, leveraging standard USB HID (Human Interface Device) Power Device class specifications. When plugged in, the kernel assigns the UPS a device path under/dev/bus/usb/or creates an input event node. - Serial (RS-232): Older or industrial UPS models use
serial connections accessed via
/dev/ttyS*or USB-to-serial adapters (/dev/ttyUSB*). These communicate over vendor-specific serial protocols or simple contact-closure signaling. - Network (SNMP / Network Management Cards): Enterprise UPS units equipped with network interface cards communicate over local networks without physical host cables, typically utilizing SNMP (Simple Network Management Protocol) or proprietary vendor protocols over TCP/IP.
User-Space Daemons
Because the Linux kernel handles only the raw device transmission, user-space monitoring software is required to translate vendor-specific data, track power status, and trigger system actions. The two most common tools are:
Network UPS Tools (NUT)
NUT is the standard, vendor-neutral suite for Linux. It uses a modular architecture split into three layers:
- Drivers: Specific binaries (such as
usbhid-upsorsnmp-ups) that talk directly to the hardware and standardize the data. - Server (
upsd): A daemon that reads from the drivers and exposes the power status locally or over the network via a uniform TCP interface. - Client (
upsmon): A daemon running on the protected machine that monitorsupsd. If the server reports a power event,upsmoninitiates protective workflows.
apcupsd
apcupsd is a specialized, lightweight daemon tailored
for APC (Schneider Electric) hardware. It operates as a unified daemon
handling communication, monitoring, and shutdown scripting
simultaneously, supporting USB, serial, and master-slave network
setups.
Event Handling and State Changes
UPS software continuously polls the unit for operational state changes. Typical states include:
- ONLINE: Normal operation; the system is running on utility mains power.
- ONBATT: Utility power has failed; the system is running on battery reserves.
- LOWBATT: The battery charge has dropped below a defined threshold or calculated runtime limit.
- COMMFAILURE: The system has lost communication with the UPS hardware.
When a state transition occurs, the daemon runs alerting utilities
(such as wall to message logged-in users) and starts
configurable timers.
The Graceful Shutdown Sequence
The most critical function of the Linux-UPS interaction is avoiding filesystem corruption and data loss through a coordinated shutdown:
- Critical Threshold Reached: Once
LOWBATTis signaled or theONBATTtimer expires, the client daemon issues an emergency shutdown command (often calling/sbin/shutdownor sending an instruction viasystemd). - Service Termination: The system transitions through target runlevels, stopping databases, terminating active processes, and unmounting storage volumes.
- Filesystem Sync: Linux flushes remaining caches to disk and remounts root filesystems as read-only.
- Power-Off Signal (UPS Inverter Cut): Near the end
of the init/systemd shutdown sequence, a final script signals the UPS
daemon to issue a delayed power-off command directly to the UPS hardware
(e.g.,
upsdrvctl shutdown). - Reboot on Power Restoration: The UPS cuts power to its outlets, turning off the host system cleanly. When mains power returns, the UPS restarts its outlets, prompting modern computer motherboards (configured to restore power state in BIOS/UEFI) to boot Linux automatically.