Tuesday, March 23, 2010

Laptop battery remaining time vs. uptime

Question: Why is it that after 15 minutes' uptime, Ubuntu thinks my laptop battery will last another 3 hours, but all it ever manages in total is 2:55 (at most)?

Here's a shell script which writes the current uptime and the estimated remaining time in a file, every minute. Hopefully this can shed some light on the matter. At least we can draw a nice graph afterwards!

You may need to sudo apt-get install acpi first.

/bin/echo "up, remaining" > $filename

while true; do
# extract h:mm from uptime output:
# try to find (h)h:mm first, then try to find 'xx min' and print as 0:xx
up=`/usr/bin/uptime | sed 's/^.*up \+\([0-9]\+:[0-9]\{2\}\).*/\1/g' | \
sed 's/^.*up \+\([0-9]\+\) \+min.*/0:\1/g'`

# extract h:mm from acpi 'hh:mm:ss' output
remaining=`/usr/bin/acpi | sed 's/^.* \+[0-9]\([0-9]:[0-9]\{2\}\):[0-9]\{2\}.*/\1/'`

# write output to file and screen
echo $up, $remaining | tee -a $filename

# interval between polls (in seconds)
sleep 60


Update: Here's a graph:

Uptime on X axis, remaining time (blue) and total time (orange) on the Y axis. Polls every minute. You can see the prognosis is optimistic at the beginning.


Kevin Renskers said...

Nou, waar is de mooie graph dan?

Kees Hink said...

Hier istie!