Notifying When Battery is Full in Linux


Case: I want to be notified by a sounding popup when the battery of my laptop is fully charged. I'm using Debian Linux and Gnome 3.

Requisites: The following applications are installed: sh, upower, paplay, notify-send.

Steps:

  • Open a text editor and put this code (adapted from here):
 #!/usr/bin/env bash  
 while true  
 do  
   export DISPLAY=:0.0  
   battery_percent=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -P -o 'percentage:     [0-9]+(?=%)' | grep -P -o [0-9]*)  
   if on_ac_power; then  
      echo $on_ac_power  
     if [ "$battery_percent" -gt 92 ]; then  
           paplay /usr/share/sounds/freedesktop/stereo/suspend-error.oga  
       notify-send --urgency CRITICAL "Battery full" "Level: ${battery_percent}% "  
     fi  
   fi  
   sleep 60 #  
 done  
  • Save the file with .sh extension in the user's home directory, choice an understandable name, for example: /home/ronald/BatteryCharged.sh
  • Edit the file named .profile of the user's home directory, add at the bottom of the file the line below and save it: 
 add sh /home/ronald/BatteryCharged.sh &  
  • Reboot the laptop and login.

Results: When the battery percentage reaches higher than 92% you will see a popup in the screen and a sound will rise, this will happen each minute until you unplug the power cable.
 
Battery popup in the screen

 

Comments

Popular posts from this blog

Install TWRP Recovery in Xiaomi Redmi 6A

I Need to Send an Automatic Email with Lotus Notes

Turn Horizontal Visio Swimlanes into Vertical Ones