How to Use the Command 'systemd-ac-power' (with examples)
- Linux
- December 17, 2024
The command systemd-ac-power
is a utility provided by the systemd suite of tools that helps users check if their computer is connected to an external power source, such as an AC adapter. This tool can be especially useful for devices like laptops that rely on battery power when not connected to an external source. By using this command, you can programmatically determine whether your device is currently drawing power from an external source, allowing for optimized power management and automation of tasks based on power status.
Use case 1: Silently Check Power Connection Status
Code:
systemd-ac-power
Motivation:
In many situations, scripts or applications need to make decisions based on whether the device is connected to a power source. For instance, a software update might only be permitted if the device is connected to AC power, avoiding potential battery drain. This simple command serves well in automation scripts where unnecessary output is undesirable, and where the main goal is simply to check the power status silently.
Explanation:
The command systemd-ac-power
works by checking your device’s connection to external power and then, silently (without producing output), returns an exit status based on the result. If the exit code is 0
, it indicates that the device is powered by an external source. If the device is running on battery or not connected to a power source, it will return a non-zero exit status. The lack of additional flags or verbosity makes it perfect for scripting, where minimal overhead and quick checks are crucial.
Example output:
Since the command is designed to run silently, there is no visible output when it is executed. However, you can check the status code which is returned. This can be done in a shell script or manually checked in a shell like this:
systemd-ac-power
echo $? # Outputs: 0 if connected to AC, otherwise some non-zero number
Use case 2: Verbosely Print Power Connection Status
Code:
systemd-ac-power --verbose
Motivation:
There are instances where a more human-readable output is desired outside of the programming context, such as when a system administrator quickly wants to check the power status manually without interpreting exit codes. The --verbose
flag provides this clarity by explicitly stating the power connection status, making it easier to understand at a glance. This is especially useful when diagnosing power issues or when the power status needs to be logged in a human-readable format.
Explanation:
The --verbose
flag is an option provided by the systemd-ac-power
command that, when used, provides a more explicit output by printing either “yes” or “no” to the standard output. “Yes” signifies that the device is connected to AC power, whereas “no” indicates it is running on battery. This enables a straightforward interpretation of the device’s power status, avoiding the need to check exit codes and look up what they mean, making it appropriate for contexts where readability is prioritized.
Example output:
When running with the --verbose
option, you’ll receive output directly in the terminal that could look like:
yes
or
no
depending on whether the device is connected to AC power or not.
Conclusion:
Understanding how to use the systemd-ac-power
command empowers users and system administrators to incorporate power status checks into their daily tasks and automation scripts. Whether silently checking through exit codes or through explicit “yes” or “no” output, the command offers straightforward functionality to efficiently determine whether a device is externally powered. This not only aids in energy management strategies but also ensures that battery-depending devices can optimize their operational tasks when necessary.