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
Thank you for this great article, it helps me a lot. But, I need the low battery notification only if the battery state status is in Discharging mode. So, I edited the code looks like this:
#!/bin/bash
while true
do
battery_state_status=`acpi -b | grep -P -o ‘Discharging’`
battery_level=`acpi -b | grep -P -o ‘[0-9]+(?=%)’`
if [ $battery_state_status == “Discharging” ]; then
if [ $battery_level -le 20 ]; then
zenity –warning –title=”Battery Low” –text=”Battery Level now is ${battery_level}%” –no-wrap –timeout=8
fi
fi
sleep 30s
done
I used zenity to ensure the notification will still appear and pop up, even if the application is in fullscreen mode.
I’m having trouble getting around this.
There’s this E12 error where I can’t open the file for writing, kindly assist
Which file are you referring?
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!
You are welcome
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.
levgen, Thanks for the information. I will check other alternatives and update the article accordingly.