Background
An old-ish laptop running a similary old and unconventional version of Linux (neither Debian-based nor any of the other top guns) running an even more obscure piece of software. This laptop is typically used out in the field and, as such, needs to run on its own battery, as the availability of power for charging is shoddy at best. The OS is primarily CLI with the exeption being the aformentioned software, but no big clunky desktop environment is used. The request came in for a simple way to get the battery status of the laptop, both while charging and when in the field, such that the operator can avoid running into unfortunate shut downs due to battery mismanagement.
Research and Resources
Relevant documentation: https://www.kernel.org/doc/Documentation/power/power_supply_class.txt
Fortunately, the laptop already had the power supply class and working drivers installed. Talk about spending all my luck… The documentation dates the power supply class to mid 2017, though this laptop predates that. I’m not sure if it was added or last updated in 2017, and I am definitively not opening cans of worm unnecessarily here, so lets continue. The OS had all the relevant info available, just not readily enough.
Solution
A simple bash script stored at /usr/bin/battery
with execution priveleges and the following content:
#!/bin/bash
Charging=$(cat sys/class/power_supply/AC/online)
if [ $Charging -eq 1 ]; then
echo "AC: Connected to Charger"
else
echo "AC: Running on Battery"
fi
BAT0C=$(cat /sys/class/power_supply/BAT0/capacity)
BAT1C=$(cat /sys/class/power_supply/BAT1/capacity)
echo "BAT 0: $BAT0C"
echo "BAT 1: $BAT1C"
Result
A simple command, battery
, that autocompletes and gives information about the current charging status and battery status in percent. Example as follows:
[user@device ~]$ battery
AC: Running on Battery
Bat 0: 42
Bat 1: 93
[user@device ~]$
The only drawback to this solution is that the operator has to be logged in to get the battery status. It is possible to automatically run the same script late in the startup process so that it will be visible at the login prompt, however I view it as a benefit for the operator to not be told before login. This way the user “has” to login to check the battery status before taking the laptop out in the field, thereby validating if the user is able to login at all before taking off.
Though it is tempting to add more complexity, such as estimating times for remaining usage and until batteries are fully charged, the demand/requirement simply is not there. As such; KISS.