


How To Monitor Battery Level And Get Notifications On Linux Using battmon
Mar 24, 2025 am 10:23 AMKeeping 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 thresholdsHIGH_THRESHOLD=95LOW_THRESHOLD=10LOGFILE="/tmp/battmon.log"# Get the battery levelLEVEL=$(acpi -b | awk -F', ' '{print $2}' | tr -d '%,')# Ensure LEVEL is a valid numberif [[ "$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> fifi
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:
- How To Check Laptop Battery Status And Level In Terminal 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:
- How To Optimize Laptop Battery Life With TLP In Linux
The above is the detailed content of How To Monitor Battery Level And Get Notifications On Linux Using battmon. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Mastering Text Manipulation With the Sed Command

Pilet: A Modular, Portable Mini-Computer Powered by Raspberry Pi

How To Count Files And Directories In Linux: A Beginner's Guide

Linux Kernel Source Code Surpasses 40 Million Lines

The Secret Weapon to Supercharge Your Linux System With Liquorix Kernel

Building Your Own Ubuntu Personal Cloud: A Step-by-Step Guide to Creating a Secure Data Haven

How To Add A User To Multiple Groups In Linux

System76 Introduces Meerkat Mini PC: Big Power in a Tiny Package
