Trying to write some kind of automatic behaviour to make laptop sleep when battery charge is, say, 5% remaining - instead of it shutting down and losing work.
Depending on your laptop, and the condition of the bsttery, 5% may be cutting it a bit fine. But anyway ...
I was thinking it could be done with a script which checks battery power levels using `apm` or `acpiconf -i 0/1` (dual battery) to check levels every 2-3 minutes using cron job.
You could do it that way. In ancient days of apm, you could configure that directly, buy nobody has run apm for a very long time AFAIK.
So it's parsing the output of acpiconf. Can you provide a sample, running on battery, as output varies with model?
I forget, what make/model?
Does it consistently empty battery 0 before battery 1?
Is there a better and more elegant way to achieve the same? Maybe acpi/apm emit some kind of events?
Yes, via ACPI CMBAT events to
devd(8), if your ACPI supports these.
E.g. on my Thinkpad X200 the embedded controller (EC) issues CMBAT events at 100, 80, 20, 10, 5 or 3% (reported*) charge, charging or discharging.
So my devd.conf entry (better as /etc/devd/my.conf) is:
Code:
notify 10 {
match system "ACPI";
match subsystem "CMBAT";
action "/root/bin/acpi_cmbat $notify";
};
and /root/bin/acpi_cmbat is:
Code:
#!/bin/sh
LOGGER="logger -t acpi_cmbat -p daemon.notice"
$LOGGER "CMBAT notify = $1" # always 0x80
/root/bin/x200stat >> /root/acpi_cmbat_events.log
exit 0
x200stat emits all sorts of info, but relevant here is a timestamp, CPU load, fan & thermal data, and battery stats parsed from acpiconf -i0, e.g:
critical discharging capacity: 7% time: 0:09
rate: 13332 mW voltage: 11260 mV
*reported battery state assumes you are keeping your battery/s calibrated with an occasional deep discharge / recharge cycle, otherwise values can differ significantly from reality.
Have fun.