How about including ACPI power status monitoring. Battery status for instance. And initiate shutdown at x%?
The following is true for my Arch Linux, pathes also match my debian. But since I only have access to a Arch based battery-powered device, it might neither work on every distribution nor on Mac.
You can use the program type with path /usr/bin/bash -c 'exit $(cat /sys/class/power_supply/BAT0/capacity)'. So the exit status is now your battery percentage.
check program shutdownAt10 with path "/usr/bin/bash -c 'exit $(cat /sys/class/power_supply/BAT0/capacity)'" timeout 1 seconds
if status < 11 then exec "/usr/bin/shutdown -h +1 'Shutdown by monit due to low battery in 60s'"
This schedules a shutdown in 1 minute (+1) and will halt your system (-h). It will also post a message on any tty available.
Since this just uses the shutdown command, you can avoid the shutdown by calling sudo shutdown -c on a bash.
Keep some more things in mind:
monit keeps running, so the next cycle will most likely relaunch the shutdown sequence
lets assume you start up your laptop at 8% with a power supply connected: It will still shutdown because it's battery-% is under 11%
PS: This technique might be used with any binary that returns a number only, since exit only allows numerical exit codes.
@Henning Bopp Hi wrote a small utility to check the main battery status under linux and windows.
It exits with code 255 if there isn’t a battery or it is charging.
Otherwise it exits with the value of the percentage of charge, 0 to 100.
How can I submit the source code to @Tildeslash@Tilde development so they can include it with monit?
Interesting, though Monit is usually run on servers without a battery so the use case is maybe limited and best served via a script and check program. We are reluctant to include code unless it works on all supported platforms.
@Tildeslash The utility supports freebsd, linux, windows. I agree it is better as an external utility. If you surely adopt this utility, I will look at macos development.
No need. We already have the code for macOS battery check which we use in the Monit Widget. It is not trivial if you haven’t done any macOS sysdev before. Anyway, as mentioned above, let's try to use check program and a script when possible.