Thursday, November 22, 2012

Monitoring battery status on Linux using python

On laptops powered by Linux, the battery status is exposed by the kernel using the file on /proc pseudo filesystem. The file that you could use to monitor on most laptops is /proc/acpi/battery/BAT0/state. Listed below is a simple python code snippet that scans this file every 1 second, collects the necessary statistics and prints the remaining charge on your laptop battery.

You can customise this code or optimise it to your liking. For convenience, you can add this script to your startup applications. So whenever you log in to your lappy, it gets auto started. You can also modify it to show you the alert to unplug your laptop from the AC adaptor when the battery is full.