Linux: get UPS remaining battery time

If you have a UPS connected to your Linux box maybe you need some simple way to get how much power is left in the batteries, i’ve written a simple tool to get this information (note: you need Nut to be installed, configured and running).


# device to check using "nut" utilities

# chars to be added at the end of output
CHARS=" | "

# displays battery remaining %
# echo "$( LANG= upsc $UPSDEV | grep battery.charge: | awk '{print $2}' )${CHARS}"

displays battery time remaining, or "infinite" if battery is not discharging
STS=$(LANG= upsc $UPSDEV | grep ups.status: | awk '{print $2}' | grep -i DISCHRG )
SEC=$(LANG= upsc $UPSDEV | grep battery.runtime: | awk '{print $2}' )
if [ "$STS" = "" ]; then
echo "∞${CHARS}"
echo "$(date -d@$SEC -u +%Hh:%Mm)${CHARS}"

I saved (and set as executable) the script in /usr/local/bin/ups-status and then i used the script as source information for xfce4genmon-plugin to have data visible in my main Xfce panel.

Just a final note: do not forget to customize the code according to you needing (set ups identification in UPSDEV, set extra text to be displayed in CHARS and comment/decomment code to get the info you want – battery % or battery time – )

Leave a comment