Battery Warning for MacOS

Since I started working from home on a multi-monitor setup, my Mac kept dying because I don't notice the tiny pop-up warning message.

I tried a lot of battery monitor apps but none of them worked. A much better solution was good old crontab alone.

Shell Script to Measure Battery Level

In a Terminal, execute this command:
sudo nano /usr/local/bin/BatteryWarn
Enter this code:
#!/bin/bash

bper=$(/usr/sbin/ioreg -l | /usr/bin/awk '$3~/Capacity/{c[$3]=$5}END{OFMT="%.3f";max=c["\"MaxCapacity\""];print(max>0?100*c["\"CurrentCapacity\""]/max:"?")}')

bpern=$(echo "$bper" | cut -d. -f 1)

if [ $bpern -lt 15 ] ; then
    say "danger danger battery low"
fi
Press Ctrl+X, Y, Enter to save the file.

Crontab to Test Every Minute

In a Terminal, execute this command:
EDITOR=nano crontab -e
Enter this code:
*/1 * * * * /usr/local/bin/BatteryWarn
Press Ctrl+X, Y, Enter to save the file.

Now your Mac will talk to you, reminding you every minute, when the battery is low!

Posted 7-20-2020