05 June 2014

checking the battery status through terminal

With no GUI on the Ubuntu side of my Samsung Chromebook, I need a way to check how much power is in the battery. The directory where all the information is stored is:

/sys/class/power_supply/sbs-104-000b/

For other systems, the path is often power_supply/BAT0/. Either way, you get a listing of several files.

Here is the script I wrote to display some battery information:



#!/bin/bash

DIR=/sys/class/power_supply/sbs-104-000b/

STATUS=`cat $DIR/status`
POWER _NOW=`cat $DIR/charge_now`
POWER_MAX=`cat $DIR/charge_full`

PERC=$(((1000*$POWER_NOW)/$POWER_MAX))
PERC=${PERC:0:2}.${PERC:2:1}

echo ""
echo "    Battery status: $STATUS"
if [ $STATUS == "Discharging" ]; then
    echo "    Power level: $PERC%"
    TIME=`cat $DIR/time_to_empty_avg`
    HOUR=$(($TIME/60/60))
    MIN=$((TIME/60 - 60*$HOUR))
    echo "    Time remaining: ${HOUR}h ${MIN}m"
fi
if [ $STATUS == "Charging" ]; then
    echo "    Power level: $PERC%"
    TIME=`cat $DIR/time_to_full_avg`
    HOUR=$(($TIME/60/60))
    MIN=$((TIME/60 - 60*$HOUR))
    echo "    Time remaining: ${HOUR}h ${MIN}m"
fi
echo ""



Alternatively, you could let PERC just be the value in $DIR/capacity, but it only gives an integer percentage, I like the extra accuracy with the decimal. From what I've seen, STATUS can either be "Discharging," "Charging," or "Full". When first going on battery power or first plugging in, the value for TIME will be off, just wait and it will get where it needs to be.

I've noticed on my other laptop the time files weren't in the directory. These could be created manually, which I will try to do some day.

No comments:

Post a Comment