search
HomeSystem TutorialLINUXHow To Monitor Battery Level And Get Notifications On Linux Using battmon

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:

  1. The battery is fully charged (≥ 95%) – Reminds you to unplug the charger to prevent overcharging.
  2. 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.

How To Monitor Battery Level And Get Notifications On Linux Using battmon

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.

How To Monitor Battery Level And Get Notifications On Linux Using battmon

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Essential Tools and Frameworks for Mastering Ethical Hacking on LinuxEssential Tools and Frameworks for Mastering Ethical Hacking on LinuxApr 11, 2025 am 09:11 AM

Introduction: Securing the Digital Frontier with Linux-Based Ethical Hacking In our increasingly interconnected world, cybersecurity is paramount. Ethical hacking and penetration testing are vital for proactively identifying and mitigating vulnerabi

How to learn Linux basics?How to learn Linux basics?Apr 10, 2025 am 09:32 AM

The methods for basic Linux learning from scratch include: 1. Understand the file system and command line interface, 2. Master basic commands such as ls, cd, mkdir, 3. Learn file operations, such as creating and editing files, 4. Explore advanced usage such as pipelines and grep commands, 5. Master debugging skills and performance optimization, 6. Continuously improve skills through practice and exploration.

What is the most use of Linux?What is the most use of Linux?Apr 09, 2025 am 12:02 AM

Linux is widely used in servers, embedded systems and desktop environments. 1) In the server field, Linux has become an ideal choice for hosting websites, databases and applications due to its stability and security. 2) In embedded systems, Linux is popular for its high customization and efficiency. 3) In the desktop environment, Linux provides a variety of desktop environments to meet the needs of different users.

What are the disadvantages of Linux?What are the disadvantages of Linux?Apr 08, 2025 am 12:01 AM

The disadvantages of Linux include user experience, software compatibility, hardware support, and learning curve. 1. The user experience is not as friendly as Windows or macOS, and it relies on the command line interface. 2. The software compatibility is not as good as other systems and lacks native versions of many commercial software. 3. Hardware support is not as comprehensive as Windows, and drivers may be compiled manually. 4. The learning curve is steep, and mastering command line operations requires time and patience.

Is Linux hard to learn?Is Linux hard to learn?Apr 07, 2025 am 12:01 AM

Linuxisnothardtolearn,butthedifficultydependsonyourbackgroundandgoals.ForthosewithOSexperience,especiallycommand-linefamiliarity,Linuxisaneasytransition.Beginnersmayfaceasteeperlearningcurvebutcanmanagewithproperresources.Linux'sopen-sourcenature,bas

What are the 5 basic components of Linux?What are the 5 basic components of Linux?Apr 06, 2025 am 12:05 AM

The five basic components of Linux are: 1. The kernel, managing hardware resources; 2. The system library, providing functions and services; 3. Shell, the interface for users to interact with the system; 4. The file system, storing and organizing data; 5. Applications, using system resources to implement functions.

Ubuntu Home Automation: Building a Smart Living Space with Open Source ToolsUbuntu Home Automation: Building a Smart Living Space with Open Source ToolsApr 05, 2025 am 09:19 AM

Opening a new chapter in smart home: Open source home automation system based on Ubuntu Smart home technology has revolutionized the way we interact with our living spaces, bringing convenience, safety and energy efficiency to our daily lives. From remote control of lights and appliances, to monitoring security cameras and automated climate control, smart home technology is becoming increasingly popular. However, many business smart home systems have limitations: high costs, privacy issues, and limited compatibility. Fortunately, open source software solutions combine the power of Ubuntu to provide an alternative – allowing users to create a customizable, cost-effective and secure smart home ecosystem. This guide will explore how to set up a home automation system using Ubuntu and open source tools.

Linux vs. Windows: What's the difference in 2025?Linux vs. Windows: What's the difference in 2025?Apr 05, 2025 am 09:05 AM

Linux vs. Windows: A 2025 Comparison Thinking about switching from macOS or Windows? Linux might be the answer. While macOS users will find a relatively smooth transition (due to macOS's UNIX core), Windows users will need to adapt. This guide hig

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment