How to get Battery status notification, when the battery is Full or Low?

Linux OS in Laptops are good for Nix users, but it often drains the battery.

Have tried many Linux operating systems, but long battery life is not evident like Windows.

Recommended to avoid charging a battery for a longer duration which might affect the battery hence unplug the power cable when it reaches 100% charge limit.

There is no default application to notify us, when the battery is fully charged or discharged. To receive the notification, we rely on the third-party application.

Initially, preferred to use Battery Monitor application to receive notification, but this app was deprecated.

To overcome the third party app usage, I have created a shell script to receive the notification.

Laptop battery charging and discharging status can be identified using the following two commands.

  • acpi
  • upower

Using acpi command.

$ acpi -b
Battery 0: Discharging, 71%, 00:58:39 remaining

Using upower command.

$ upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -w 'state|percentage' | awk '{print $2}'
discharging
64%

Method-1: Receive an alert, when the battery level is above 95% or below 20%

This shell script runs in the background on startup and checks the battery status every minute and then sends a notification when the battery level is charged above 95% or discharged less than 20%.

The alert will not get turned off, until your battery is over 20% or less than 95% charged.

$ sudo vi /opt/scripts/battery-status.sh

#!/bin/bash
while true
do
  battery_level=`acpi -b | grep -P -o '[0-9]+(?=%)'`
   if [ $battery_level -ge 95 ]; then
      notify-send "Battery Full" "Level: ${battery_level}%"
      paplay /usr/share/sounds/freedesktop/stereo/suspend-error.oga
    elif [ $battery_level -le 20 ]; then
      notify-send --urgency=CRITICAL "Battery Low" "Level: ${battery_level}%"
      paplay /usr/share/sounds/freedesktop/stereo/suspend-error.oga
  fi
 sleep 60
done

Once the script is ready, set the executable permission.

$ sudo chmod +x /opt/scripts/battery-status.sh

Finally, add the script to the bottom of the user profile file. For system-wide users, add the script on the /etc/profile file. This profile file will allows the system to kick the script when the system starts.

$ vi ~/.profile 

sh /opt/scripts/battery-status.sh &

Reboot your Linux system to verify the notification status.

$ sudo reboot

Method-2: Receive notification, when the battery level is charged (above 95%) or discharged (below 20%)

This script is similar to the above script, but it is in Sync with the AC adapter.

If the AC adapter is plugged in and the battery is charged above 95%, it will trigger a notification with a sound, but the notification will not stop until the AC adapter is unplugged.

If AC adapter is unplugged, notification will not appear again until the battery charge drops to 20%.

$ sudo vi /opt/scripts/battery-status-1.sh

#!/bin/bash
   while true
    do
       export DISPLAY=:0.0
       battery_level=`acpi -b | grep -P -o '[0-9]+(?=%)'`
       if on_ac_power; then
           if [ $battery_level -ge 95 ]; then
              notify-send "Battery Full" "Level: ${battery_level}% "
              paplay /usr/share/sounds/freedesktop/stereo/suspend-error.oga
           fi
       else
           if [ $battery_level -le 20 ]; then
              notify-send --urgency=CRITICAL "Battery Low" "Level: ${battery_level}%"
              paplay /usr/share/sounds/freedesktop/stereo/suspend-error.oga
           fi
       fi
     sleep 60
done

Once the script is ready, set the executable permission.

$ sudo chmod +x /opt/scripts/battery-status-1.sh

Finally, add the script to the bottom of the user profile file. For system-wide users, add the script on the /etc/profile file.

$ vi ~/.profile

sh /opt/scripts/battery-status-1.sh &

Restart your system to verify the notification status.

$ sudo reboot

Conclusion

This article gives an idea of using our own script instead of using any third pary application to receive Battery status notification.

Kindly support us by sharing this article, if found helpful!

Ref: stackexchange

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

4 Comments on “How to get Battery status notification, when the battery is Full or Low?”

  1. I have been looking for an app that works like this for a long time. For M$ Win there are various solutions, in Linux, especially the widespread and dominant Debian distributions Ubuntu/LinuxMint etc..
    Linux nerds will surely get along with the script. Normal users might have problems with it. A GUI is in this respect comfortable, because easier to use or install.

    Thanks for the script!

  2. on_ac_power is a script unique to Ubuntu.
    It is better to identify if on AC power by reading values from /sys .
    Something like
    `cat /sys/class/power_supply/AC0/online`
    `AC0` part might differ depending on machine.

Leave a Reply

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