Keeping your Laptop battery in check is very important for maintaining its longevity and ensuring you're never caught off guard by a sudden shutdown. If you’re a Linux user, you can easily monitor your Laptop's battery level and receive notifications when it’s fully charged or very low using a simple Bash script called battmon.
Table of Contents
Why Monitor Battery Levels?
Overcharging your laptop battery can reduce its lifespan over time. Likewise, allowing the battery to drop too low can lead to an unexpected shutdown or hibernation, potentially causing data loss.
By setting up notifications when your battery reaches a specific charge level (e.g., 95%), you can unplug your device to prevent overcharging. Similarly, alerts for a critically low battery (e.g., 10%) remind you to plug in the charger before your system shuts down.
What is battmon?
battmon
is a simple yet effective battery monitoring script for Linux. It automatically checks your battery level and sends desktop notifications when:
- The battery is fully charged (≥ 95%) – Reminds you to unplug the charger to prevent overcharging.
- The battery is critically low (≤ 10%) – Alerts you to plug in the charger before the system shuts down.
You can, of course, customize the battery level percentage as you wish.
battmon uses acpi
to fetch battery details and notify-send
to display alerts on the desktop. All actions are logged into /tmp/battmon.log
for easy troubleshooting.
You can run the script in the background using a cron job, ensuring that you always receive timely notifications without manually checking your battery status.
Battmon is completely free to use script written in Bash.
Set Up Laptop Battery Alerts Using battmon on Linux
Step 1: Install Required Packages
As I already mentioned, the battmon script uses the acpi
command to check the battery level and notify-send
to display a desktop notification when the battery is fully charged or critically low.
First, install these tools. If you're on Debian-based systems, run the following command in your terminal:
sudo apt install acpi libnotify-bin
Step 2: Download battmon script
Create a file called battmon.sh
with the following contents:
#!/usr/bin/env bash
# ------------------------------------------------------------------
# Script Name: battmon.sh
# Description: A Simple Bash Script for Battery Level Charge
# Notifications
# Website: https://gist.github.com/ostechnix
# ------------------------------------------------------------------
# Define thresholds
HIGH_THRESHOLD=95
LOW_THRESHOLD=10
LOGFILE="/tmp/battmon.log"
# Get the battery level
LEVEL=$(acpi -b | awk -F', ' '{print $2}' | tr -d '%,')
# Ensure LEVEL is a valid number
if [[ "$LEVEL" =~ ^[0-9]+$ ]]; then
# Check for high battery level
if [ "$LEVEL" -ge "$HIGH_THRESHOLD" ]; then
echo "$(date) - Battery at $LEVEL%. Sending high battery notification..." >> "$LOGFILE"
DISPLAY=:0 XDG_RUNTIME_DIR=/run/user/1000 notify-send -t 0 "Battery Full" "Your battery is now fully charged." >> "$LOGFILE" 2>&1
fi
# Check for low battery level
if [ "$LEVEL" -le "$LOW_THRESHOLD" ]; then
echo "$(date) - Battery at $LEVEL%. Sending low battery warning..." >> "$LOGFILE"
DISPLAY=:0 XDG_RUNTIME_DIR=/run/user/1000 notify-send -t 0 "Battery Low" "Your battery is critically low. Please plug in your charger!" >> "$LOGFIL>
fi
fi
Pro Tip: Customize the battery threshold in the scripts to suit your preferences. For example, you can set it to 90% or 80% if you want to unplug your device earlier.
In future, we may improve this script. Please check our GitHub Gists page once in a while to get the update-to-date battmon script.
Step 3: Make the Script Executable
After saving the file, you need to make it executable so that you can run it as a script. Use the following command:
chmod +x battmon.sh
Step 4: Move the Script to your $PATH
To make the script easily accessible from anywhere in your terminal, move it to a directory that's included in your system’s PATH. The /usr/local/bin
directory is a common choice:
sudo mv battmon.sh /usr/local/bin/battmon
Step 5: Schedule the Script with Cron
You’ll want the script to run automatically at regular intervals. This is where cron jobs come in handy.
A cron job allows you to schedule the script to run every 5 minutes, so you’re constantly informed about your battery status.
To set up the cron job, run:
crontab -e
Add the following line:
*/5 * * * * /usr/local/bin/battmon >> /tmp/battmon.log 2>&1
This line tells cron to run the script every 5 minutes and log the output to /tmp/battmon.log
.
Replace /usr/local/bin/battmon
with the actual path to your script.
Now the script will continuously run in the background and check the battery level every 5 minutes. If the battery reaches 95%, it will automatically notify you as shown in the screenshot below. You can then unplug the charging cable.
Liewise, the battmon
script alerts you when the battery level is critically low (below 10%), so you can plug in the power cable and prevent system interruptions.
Step 6: Check the Logs
If you want to troubleshoot or check the history of notifications, you can examine the log file. The log file will contain entries whenever a notification is sent:
cat /tmp/battmon.log
You’ll see logs like this:
Monday 10 March 2025 03:10:01 PM IST - Battery at 99%. Sending high battery notification...
Monday 10 March 2025 03:15:01 PM IST - Battery at 96%. Sending high battery notification...
Monday 10 March 2025 06:10:01 PM IST - Battery at 6%. Sending low battery warning...
Monday 10 March 2025 06:15:01 PM IST - Battery at 9%. Sending low battery warning...
There are also more ways to check the battery level. The following article includes 5 different methods to check the battery level in Linux:
Pick any method from the list and update the script accordingly.
Conclusion
battmon is a simple yet effective tool for monitoring your battery level on Linux. By using battmon script, you can easily monitor your battery level and receive notifications when it’s fully charged or critically low.
This not only helps you maintain your battery’s health but also ensures you’re always aware of your device’s power status.
If you have any other useful tips for battery management on Linux, please let us know in the comments. I will check and update the guide accordingly.
Related Read: