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:

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:

  1. Drivers: Specific binaries (such as usbhid-ups or snmp-ups) that talk directly to the hardware and standardize the data.
  2. Server (upsd): A daemon that reads from the drivers and exposes the power status locally or over the network via a uniform TCP interface.
  3. Client (upsmon): A daemon running on the protected machine that monitors upsd. If the server reports a power event, upsmon initiates 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:

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:

  1. Critical Threshold Reached: Once LOWBATT is signaled or the ONBATT timer expires, the client daemon issues an emergency shutdown command (often calling /sbin/shutdown or sending an instruction via systemd).
  2. Service Termination: The system transitions through target runlevels, stopping databases, terminating active processes, and unmounting storage volumes.
  3. Filesystem Sync: Linux flushes remaining caches to disk and remounts root filesystems as read-only.
  4. 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).
  5. 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.