Battery capacity monitoring

Introduction
Battery state can be monitored periodically. When the capacity is below certain level, an action can be triggered.

Battery capacity change doesn't trigger udev events, so udev can't be used for this purpose. Monitoring is performed periodically, using crond to perform the job.

Preparation
Create an executable script for action at various capacity level. The example script below will blink the notification light when the battery is at 25 percent and will blink the notification light again when the battery is at 15 percent and shutdown. The script below is for Xiaomi Redmi 1S (xiaomi-armani), so modification of sysfs path is needed for use on another devices.

Place the script above as check_battery_state.sh inside /etc/periodic/2min directory. Create the directory if needed.

Make the script file executable.

Adding a crontab entry to do the job
A crontab entry is needed to perform the scheduled battery check. Modify root's crontab to do the scheduled task.

The command will open root's crontab file /var/spool/crontabs/root inside vi text editor.

Note that the busybox crontab command doesn't parse EDITOR environment variable, so if another text editor is pereferred, open the root crontab file directly.

Add a line to indicate that a new 2 minutes action will be performed.


 * 1) min   hour    day     month   weekday command
 * /2    *       *       *       *       run-parts /etc/periodic/2min

Enabling crond service
To enable crond service so that crontab will be executed, execute the following command.

The crond service will be started automatically after rebooting.

To start the service immediately, run the following command.

The crond will execute check_battery_state.sh every 2 minutes and the predefined low battery actions will be performed.