I'm trying to automate desktop setup, and as part of that I'd like to install a battery monitor package only if the machine currently has a battery attached (UPSes and other external batteries are not relevant). How can I go about detecting this?
My laptop has a directory /sys/class/power_supply/BAT1
and my desktop has nothing in the /sys/class/power_supply
directory. Is that a rock-solid indicator on a system with sysfs? The reference doesn't mention BATn
directories; are they likely a vendor-specific feature?
Best Answer
The presence of battery information in
/sys/class/power_supply
is a reliable indicator that the system supports a battery, and it's a standard Linux feature, but it won't always be called the same thing. Yours is calledBAT1
. I am looking at one here that is calledbattery
. Also, not all entries in/sys/class/power_supply
are batteries.Look for all files matching the pattern
/sys/class/power/supply/*/type
. If at least one of them contains the workBattery
, there is a battery. Other possible values fortype
areMains
andUSB
, maybe more.One other thing that is possible is that the system accepts a battery but the battery has been physically removed. In that case, the
Battery
power supply will still show up. How you can detect this might be system-dependent, but try readingcapacity
or something and see if you get a read error, which probably indicates that the battery has been removed.Details: Documentation/power/power_supply_class.txt