Laboratorium Komputerowe Progmar
Marcin Załęczny

We are using cookies in the page. If you use the page you agree for the cookies.      Close

Monitoring battery discharge state in Ubuntu

Details of battery state we can examine by following command: upower -i $(upower -e | grep BAT)

The battery discharge state (remaining load percentage) we can check as below: upower -i $(upower -e | grep BAT) | grep -E "percentage" | awk '{ print $2 }'

Script alerting about hardly battery discharging:

#!/bin/sh

BATTERY_FILE=$(upower -e | grep BAT)

while true; do
    STATE=$(upower -i "$BATTERY_FILE" | grep -E "state" | awk '{ print $2 }')
    PERCENTAGE=$(upower -i "$BATTERY_FILE" | grep -E "percentage" | awk '{ print $2 }' | sed 's/%//g')

    if [ "x$STATE" = "xdischarging" ]; then
        echo "Battery discharging: ${PERCENTAGE}% left"
        if [ $PERCENTAGE -lt 8 ]; then
            mpg123 ~/Download/beep-02.mp3
        fi
    fi
    sleep 5
done;