How to use the command 'systemd-ac-power' (with examples)
- Linux
- December 25, 2023
This article will provide examples of how to use the command systemd-ac-power
which is used to report whether the computer is connected to an external power source. The command allows users to check if the system is running on AC power or not.
Use case 1: Silently check and return a 0 status code
Code:
systemd-ac-power
Motivation:
This use case is useful when you only need to check the status of the power source without any additional information. By running the command systemd-ac-power
, you can quickly determine if the system is connected to an AC power source or not.
Explanation:
The command systemd-ac-power
is used to check if the computer is connected to an external power source. Running the command without any arguments will silently check and return a status code. It returns a 0 status code if the system is running on AC power and a non-zero code otherwise.
Example output:
0
Use case 2: Additionally print ‘yes’ or ’no’ to stdout
Code:
systemd-ac-power --verbose
Motivation:
This use case is helpful when you want to not only check the power source status but also get a clear answer printed to the console. By using the --verbose
option with the systemd-ac-power
command, it will return ‘yes’ if the computer is connected to an external power source, and ’no’ if it’s not.
Explanation:
The --verbose
option is used with the systemd-ac-power
command to obtain a more descriptive output. When this option is used, the command will not only return the status code but also print ‘yes’ or ’no’ to stdout
depending on whether the computer is connected to an external power source or not.
Example output:
yes
Conclusion:
The systemd-ac-power
command is a handy tool that allows users to quickly determine if the computer is connected to an external power source. By using it with or without the --verbose
option, users can either obtain a status code or a ‘yes’ or ’no’ answer, depending on their needs.