Bash script to monitor MacBook charging percentage

Lithium-ion batteries, commonly found in smartphones and laptops, are prone to degradation due to full charge cycles and exposure to high temperatures. These batteries can also experience stress at both high and low extremes.

To mitigate degradation and extend the battery’s lifespan, it’s advisable to avoid extreme high and low charge levels. Keeping the battery charged within the range of 25–85% is considered the optimal “sweet spot”.

A Bash script, paired with a CronJob, is designed to prompt a text-based notification along with a default sound (low “Basso” | high “Ping”) alert based on the battery status:

  • Low Battery Alert: Notifies when the battery level drops below 25%, sending notifications at 25%, 24%, and 23%.
  • High Battery Alert: Notifies when the battery level rises above 85%, sending notifications at 85%, 86%, and 87%.

This script introduces a file called “previous_battery_percentage.txt” in the user’s home directory to track the battery status.

Script implementation

  1. Copy the script – save the provided script as a bash shell script file (e.g., “battery_check.sh“):
#!/bin/bash

# Get the user's home directory
home_dir=$HOME

# Specify the file path for the battery percentage file in the user's home directory
battery_file="$home_dir/previous_battery_percentage.txt"

# Get power source
power_source=$(pmset -g batt | awk -F"'" 'NR==1 {print $2}')

# Get current battery percentage
current_percentage=$(pmset -g batt | awk -F"\t" 'NR==2 {print $2}' | grep -o '[0-9]\+' | head -1)

# Extracting the first number from the first line of the file
previous_percentage=$(head -n 1 "$battery_file" 2>/dev/null | grep -o '^[0-9]\+')

# If the file is empty or doesn't exist, default to current percentage
if [ -z "$previous_percentage" ]; then
    previous_percentage="$current_percentage"
fi

# Check for "Battery Power"
if [ "$power_source" = "Battery Power" ]; then
    # Check for percentage drop below 25
    if [ "$current_percentage" -le 25 ] && [ "$current_percentage" -ge 23 ] && [ "$previous_percentage" -gt "$current_percentage" ]; then
        osascript -e 'display notification "Battery percentage is '$current_percentage'%." with title "Low Battery"';
        afplay /System/Library/Sounds/Basso.aiff
    fi
fi

# Check for power source not being "Battery Power"
if [ "$power_source" != "Battery Power" ]; then
    # Check for percentage rise above 85
    if [ "$current_percentage" -ge 85 ] && [ "$current_percentage" -le 87 ] && [ "$previous_percentage" -lt "$current_percentage" ]; then
        osascript -e 'display notification "Battery percentage is '$current_percentage'%." with title "High Battery"';
        afplay /System/Library/Sounds/Ping.aiff
    fi
fi

# Update the previous battery percentage file in the user's home directory with the current percentage
echo "$current_percentage" > "$battery_file"
  1. Grant permissions – make the script executable by running:
    chmod +x battery_check.sh
  1. Create a Cron job – set up a CronJob by opening Terminal and typing:
    crontab -e
  1. Define the Cron configuration – add the following line to check the battery status every minute:
    * * * * * /path/to/battery_check.sh

Save and exit the editor. This CronJob will execute the script every minute to monitor the battery percentage. You can create a custom Cron schedule expression using tools like “crontab guru“.

  1. Verify task scheduling – to verify your scheduled tasks and their execution times, use the command:
    crontab -l
CronJob Low Battery Notification
An example of triggered low battery notifications.

Feel free to tweak and expand upon this script to align with your specific requirements and enhance your device’s battery efficiency!

asterix Written by:

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *