Ubuntu – How to monitor battery condition and pop-up notification

batterycommand linemonitoringnotification

Essentially, I would like this comment into a working answer.

I know how to extract the battery percentage from How to check battery status using terminal?:

upower -i $(upower -e | grep BAT) | grep --color=never -E percentage|xargs|cut -d' ' -f2|sed s/%//

And how to pop-up a basic notification:

notify-send "battery low"

But how can I set up a (bash?) script to permanently monitor the output and send notification as per this pseudo-code:

if battery_status < 10% then notify-send "battery low" and put my system to suspended state sudo pm-suspend

Best Answer

Step one: make pm-suspend accessible to all users, no password asked

Do sudo visudo and add this line at the end of the file: yourusername ALL=NOPASSWD: /usr/sbin/pm-suspend

Source: How do I run specific sudo commands without a password?

Step two: create batwatch.desktop file:

This is the file that will launch automatically the monitoring script. The file must be stored in $HOME/.config/autostart/ folder.

[Desktop Entry]
Type=Application
Exec=/home/serg/bin/batwatch.sh
Hidden=false
NoDisplay=false
Name=Battery Monitor Script

Notice that the script is in my /home/serg/bin folder. You can use whatever folder you like, but for the sake of standards /usr/bin or /home/username/bin would be more prefered.

Source: How to run a script on startup

Step three:create the actual script, save in the same location as Exec= line

Here's the actual script. Notice, I'm using bash there, but it also should work with korn shell. I added some comments, so read those to understand what the script does exactly

#!/bin/bash

# Check if the battery is connected
if [ -e /sys/class/power_supply/BAT1 ]; then

    # this line is for debugging mostly. Could be removed
    #notify-send --icon=info "STARTED MONITORING BATERY"
    zenity --warning --text "STARTED MONITORING BATERY"

    while true;do   
            # Get the capacity
            CAPACITY=$( cat /sys/class/power_supply/BAT1/uevent | grep -i capacity | cut -d'=' -f2 )

            case $CAPACITY in
            # do stuff when we hit 11 % mark
            [0-9]|11)
                # send warning and suspend only if battery is discharging
                # i.e., no charger connected
                STATUS=$(  cat /sys/class/power_supply/BAT1/uevent | grep -i status | cut -d'=' -f2 )
                 if [ $(echo $STATUS) == "Discharging" ]; then

                    #notify-send --urgency=critical --icon=dialog-warning "LOW BATTERY! SUSPENDING IN 30 sec"
                    zenity --warning --text "LOW BATTERY! SUSPENDING IN 30 sec"
                    sleep 30
                    gnome-screensaver-command -l && sudo pm-suspend
                    break
                 fi
                ;;
            *)
            sleep 1
                continue
                ;;
            esac
    done
fi

Step four: reboot and test if the script works

For this purpose you can adjust the number [0-9]|11) to whatever value you like, for example 65) to suspend at 65%. The you will suspend only if you're not connected to power supply (i.e, not charging).

Let me know if you like this, and if it works, make sure to upvote and click the grey checkmark to the left side of my answer !

Cheers !