LinuxCommandLibrary

on_ac_power

Check if system is running on AC power

TLDR

Test if a computer is running on line power

$ on_ac_power
copy

SYNOPSIS


on_ac_power

DESCRIPTION

The on_ac_power command is a simple utility designed to determine whether the Linux system is presently connected to an AC power source. It is often employed in shell scripts or automated tasks to dynamically adjust system behavior based on power availability. For instance, scripts might use it to enable or disable power-saving features, dim the screen, or modify CPU frequency scaling when switching between battery and AC power.

The command's primary function is to return an exit status that signifies the current power state: typically 0 for AC power and a non-zero value (usually 1) for battery power. This makes it highly effective for use in conditional logic within scripts. Internally, on_ac_power typically queries the system's power management interfaces, such as the sysfs filesystem (e.g., by checking the state of /sys/class/power_supply/AC*/online) or by interacting with power management daemons like Upower. While its direct availability can vary across different Linux distributions and system versions (often being part of older power management utility packages like pm-utils), its core role in adaptive power management remains valuable.

CAVEATS

The availability and exact behavior of the on_ac_power command can vary considerably between Linux distributions and their versions. It was historically a common component of the pm-utils package. However, in more modern systems, its functionality might be integrated into or superseded by more comprehensive power management services like Upower, or it might not be provided as a standalone binary at all. Users should verify its presence on their specific system. The accuracy of its output depends entirely on the underlying kernel drivers and hardware correctly reporting the power status.

EXIT STATUS

The on_ac_power command communicates the system's power status through its exit code:

An exit status of 0 indicates that the system is currently running on AC power.
An exit status of 1 indicates that the system is currently running on battery power.
An exit status greater than 1 typically signifies an error condition or an inability to determine the power status.

This simple, clear exit status design makes on_ac_power exceptionally useful for conditional execution in shell scripts, allowing for logic like if on_ac_power; then echo 'On AC'; else echo 'On Battery'; fi.

HISTORY

The on_ac_power command emerged as a practical utility within the broader landscape of Linux power management, particularly with the rise of laptops and mobile computing. It was frequently bundled as part of the pm-utils suite, a collection of scripts aimed at managing power states for portable devices. Its development was driven by the necessity for systems to automatically adapt their operational profile (e.g., performance vs. battery life) depending on the power source. As Linux power management matured and became more integrated with desktop environments and standardized D-Bus services like Upower, the standalone role of on_ac_power sometimes shifted. While still functional where present, modern approaches often leverage more centralized power information services, sometimes rendering on_ac_power as either a legacy tool or a simple wrapper around more advanced APIs.

SEE ALSO

upower(1), acpi(1), pm-suspend(8)

Copied to clipboard