There are many ways it could be implemented, as there are many different power managment schemes implemented depending on what you have installed.
This simple one works for me on minimalistic Debian Jessie without any desktop environment, just with small and fast icewm window manager. (It is trimmed down because is just way too slow otherwise, and this way it outperforms GNOME on much better hardware)
Specifically, I DO have installed following packages:
acpi acpi-fakekey acpi-support acpi-support-base acpid pm-utils
but have NONE of the following (having purged them):
gnome* kde* systemd* uswsusp upower laptop-mode-tools hibernate policykit-1
So I just put this in /etc/cron.d/battery_low_check
(all in one line, split for readability):
*/5 * * * * root acpi --battery |
awk -F, '/Discharging/ { if (int($2) < 10) print }' |
xargs -ri acpi_fakekey 205
It is quick, low-resource-usage, and does not depend on other deamons (if fact, it will be ignored if they're active - see /usr/share/acpi-support/policy-funcs
for details).
What it does: every 5 minutes (*/5
- you can change to every minute by just using *
if you need it to check battery more often) it will poll battery status ("acpi --battery") and execute command after xargs -ri
only if battery is "Discharging" (that is, you're not connected to AC) and battery status is less than 10%
("int ($2) < 10" - feel free to tune it to your needs)
acpi_fakekey 205
will by default send KEY_SUSPEND
ACPI event (like you pressed a key on laptop requesting suspend), which will then do whatever it usually does for you (configured in /etc/default/acpi-support
) - for me it hibernates to disk.
You could use other command instead of acpi_fakekey 205
of course: like hibernate
(from hibernate package), s2disk
or s2mem
(from uswsusp package), pm-suspend-hybrid
(from pm-utils package) etc.
BTW, magic key numbers like KEY_SUSPEND=205 above are defined in /usr/share/acpi-support/key-constants
(other interesting one is probably KEY_SLEEP=142)