on_ac_power
Check if system is running on AC power
SYNOPSIS
on_ac_power
DESCRIPTION
The `on_ac_power` command is a simple utility that determines whether the system is currently running on AC power or is relying on battery power. It achieves this by examining the contents of a specific file in the `/sys/class/power_supply` directory, specifically the `online` file associated with the AC adapter. If the contents of this file are `1`, it indicates that the system is on AC power; otherwise (typically `0`), it's running on battery. The command provides a straightforward way for scripts or applications to adjust their behavior based on the power source, potentially conserving energy or modifying performance settings when running on battery.
It's most commonly found in laptop environments but may also be present on desktop systems with uninterruptible power supplies (UPS). It's often used in shell scripts for power management and battery monitoring, providing a quick and easily parsed way to check the power state.
CAVEATS
The command relies on the presence and correct configuration of the `/sys/class/power_supply` directory and the `online` file within it.
The location and specific file names may vary slightly depending on the system's hardware and kernel version. If the required files are absent or inaccessible, the command may produce an error or incorrect output.
This command only checks if an AC adapter is connected and providing power; it doesn't provide information about battery health or charging status.
RETURN VALUES
The command returns 0 if the system is on AC power and 1 if it is running on battery. Any other return value usually indicates an error.
USAGE EXAMPLE
if on_ac_power; then
echo "System is on AC power.";
else
echo "System is running on battery.";
fi
HISTORY
The `on_ac_power` command is typically part of a larger collection of power management utilities.
It was developed to provide a simple and reliable way to determine the AC power status in shell scripts without needing to parse more complex output from other system tools. Its usage has remained consistent over time, primarily focused on power management tasks.