Skip to main content

Command Palette

Search for a command to run...

Cron + pmset : A Cron-Based Battery Low Alerts on macOS

Updated
3 min read
Cron + pmset : A Cron-Based Battery Low Alerts on macOS

The Low Battery Problem

You're deep in work when suddenly your Mac shuts down. Battery dead. No warning, no heads-up—just darkness. You've plug in and wait till its back to work.

macOS has built-in low battery warnings, but they're easy to miss. They might appear as a small notification that gets buried, or you might have Do Not Disturb enabled.

But there's another problem: Batteries wear out faster when kept very low or fully charged for long periods. Lithium-ion batteries last longer if you avoid frequent deep drains and staying at 100% all the time. Occasional full charges or dips below 20% are fine, but keeping the battery roughly between 20% and 80–90% helps maintain long-term health.

What if you could get a more noticeable alert—with sound and a dialog—that actually gets your attention?

The Solution

Here's a simple bash script that monitors your battery and alerts you when it drops to 20% or below:

#!/bin/bash

# Get battery percentage and charging status
BATTERY_INFO=$(pmset -g batt | grep -Eo '\d+%' | tr -d '%')
CHARGING_STATUS=$(pmset -g batt | grep 'Now drawing' | awk -F"\'|\'" '{print $2}')

# Check conditions
if [[ "$CHARGING_STATUS" == "Battery Power" ]]; then
    if [[ $BATTERY_INFO -le 20 ]]; then
        afplay /System/Library/Sounds/Ping.aiff && osascript -e 'display dialog "Battery at 20% !\nPlug in to charge." with title "Battery Reminder" buttons {"Cancel", "OK"} default button "OK"' -e 'if button returned of result is "OK" then do shell script "pmset displaysleepnow"'
    fi
fi

How It Works

The script checks if you're on battery power and if the battery is ≤ 20%. When both conditions are met, it will play a sound and show a dialog box with "OK" and "Cancel" buttons:

  • on "OK": the screen is immediately put to sleep, helping you remember to plug in your charger.
  • on "Cancel": the alert is simply dismissed and nothing else happens.

Setting It Up

Save the script to your scripts directory and make it executable:

mkdir -p ~/scripts/notify
# Save the script as notify-batter .sh in ~/scripts/notify/
chmod +x ~/scripts/notify/notify-batter.sh

Add it to your crontab to run every 5 minutes:

crontab -e

Add this line:

# Check Battery every 5 mins
*/5 * * * * /Users/aananth.k/scripts/notify/notify-batter.sh

That's it. The script will automatically check your battery every 5 minutes and alert you when it's low.

Customization

Want to change the threshold? Just modify the percentage:

# Alert at 15% instead of 20%
if [[ $BATTERY_INFO -le 15 ]]; then

Why This Works

  • Hard to miss: Plays a sound and shows a dialog
  • Requires action: You must respond to the alert
  • Simple: Just a Bash script, no extra apps
  • Always shows: Works even with Do Not Disturb

Final Thoughts

This isn't a complex solution—it's a simple script that solves a real problem. Set it up once, and you'll never miss a low battery warning again.

Happy 🔋 saving!