I want to know when my battery charge changes, and I don't want to simply run a daemon that checks on it every five seconds.

I've tried running inotifywait -m /sys/class/power_supply/BAT1/capacity, but it doesn't register any modifications even though cat-ing it every once in a while gives different results! In fact, it only reports something when I used cat on it, or run acpi. I've also tried running inotifywait on other files in the BAT1 directory, and found out that none have been seen made modifications to - despite giving new results with cat.

So why doesn't inotifywait report modifications? And how can I get instant updates on changes in battery level if I can't use inotify?

  • I'm not sure, but it may be a special file (as they are in /sys and /proc). So it may be that the kernel basically returns the actual value as the file is read, rather than changing the file everytime. Something like a fuse fs with custom return functions.
    – rudib
    Apr 11 '20 at 19:32

As @rudib said in the comments, everything in /sys is virtual: the content of each file is created fresh from the corresponding kernel data structure whenever it's actually being read. So, there are no modifications in the sense of something writing into the file to change it.

The same goes for /proc.

Battery status notifications are available as generic Netlink messages, with family name = acpi_event and multicast group name acpi_mc_group. Unfortunately I don't know of a tool that would give easy access to Netlink messages for shell scripts, but apparently the pyroute2 tool can also decode netlink messages that contain ACPI events, so it might be useful as a Python code example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.