Linuxstar
Published on

linux acpi: Show battery information

Jonas

Jonas

2 min read

Linux ACPI: Show Battery Information

In this article, we will explore how to retrieve battery information on a Linux system using ACPI (Advanced Configuration and Power Interface). ACPI provides an interface for managing hardware components, including batteries. By accessing the ACPI data, we can obtain details such as battery status, charge level, and remaining capacity.

To access battery information, we can make use of the /sys/class/power_supply directory in Linux. This directory contains subdirectories for different power sources, including batteries. Let's take a look at an example code snippet to retrieve battery information.

$ cat /sys/class/power_supply/BAT0/uevent

The uevent file in the BAT0 directory provides information about the battery. Use the cat command to display its contents. This file contains key-value pairs, including the battery's status, capacity, and voltage.

To extract specific information, we can use commands like grep or awk. For instance, let's retrieve the current capacity of the battery.

$ cat /sys/class/power_supply/BAT0/uevent | grep "POWER_SUPPLY_CAPACITY"

This command will display the current percentage of battery capacity, allowing you to monitor how much charge is left.

By accessing additional files in the /sys/class/power_supply/BAT0 directory, you can gather more information. For example, the status file shows the battery's charging state – whether it's charging, discharging, or present but not charging.

$ cat /sys/class/power_supply/BAT0/status

Understanding battery information is crucial for managing power on a Linux system. By combining this knowledge with other scripting techniques, you can create custom scripts to notify or take action based on battery levels.

Remember to replace BAT0 with the correct identifier if your system has multiple batteries. You can find the identifiers by listing the contents of the /sys/class/power_supply directory.

In conclusion, ACPI provides essential tools for retrieving battery information on Linux systems. By exploring the /sys/class/power_supply directory and accessing relevant files, you can monitor battery status, capacity, and other details. Ensure you use your scripting skills to create personalized solutions.

Happy scripting!