Battery Capacity Logger MkII

A minor update to my battery logger script to tidy up the format of the log file and to make a slightly nicer (maintainable) script file.

#!/bin/sh
# batterylogger.sh : Log the battery capacity to a file
#
filename=/var/log/batterycapacity.log
date=`date`
capacity=`system_profiler SPPowerDataType |grep "charge capacity"`
count=`system_profiler SPPowerDataType |grep "Cycle count"`
echo ${date} ${capacity} ${count} >> ${filename}

This gives a log file output like the following

Sat 14 Nov 2009 13:45:06 GMT Full charge capacity (mAh): 4740 Cycle count: 163

To create the log file in a sensible place like /var/log/ I did the following:

sudo touch /var/log/batterycapacity.log
sudo chmod 666 /var/log/batterycapacity.log

I’ve also switched to using periodic instead of cron as I can’t seem to get cron to work reliably. To make it work with periodic I simply place my batterylogger.sh script in the directory /etc/periodic/daily/

NOTE: I’ve had a couple of issues lately with pasting from the web into a script file and finding i get the following error:
No such file or directory#!/bin/sh

I’ve found that this problem is due to binary characters in my text file. I think they are UTF-8 encoded characters that have slipped in from cutting and pasting. To solve this problem I use the command vim -b script. This will load up the file in binary mode so I can see what funny characters have found their way into my file. After deleting the offending character or characters the script functions as expected.

Leave a Reply

You must be logged in to post a comment.