Battery logging bash script

Started by leniad, July 06, 2016, 06:01:44 PM

Previous topic - Next topic

leniad

I wrote a simple script to log the battery information. It doesn't write when the battery is full and also was full in the last check, to avoid filling the log with useless information. I have it set up to run every minute with crontab.
I'm posting it here in case it's useful to anyone.

#!/bin/bash

BATT_DIR=/sys/class/power_supply/battery
LOG_FILE=/tmp/battlog.csv
OLD_STATUS=/tmp/status

if [ ! -f "$OLD_STATUS" ]
then
        echo -n "No_Status" > $OLD_STATUS
fi

if [ $(cat $BATT_DIR/status) != "Full" ] || [ $(cat $OLD_STATUS) != $(cat $BATT_DIR/status) ]
then
        echo -n $(date +"%F %R") >> $LOG_FILE
        echo -n ";" >> $LOG_FILE
        echo -n $(cat $BATT_DIR/capacity) >> $LOG_FILE
        echo -n ";" >> $LOG_FILE
        echo -n $(cat $BATT_DIR/status) >> $LOG_FILE
        echo -n ";" >> $LOG_FILE
        echo -n $(cat $BATT_DIR/voltage_now) >> $LOG_FILE
        echo -n ";" >> $LOG_FILE
        echo $(cat $BATT_DIR/current_now) >> $LOG_FILE
fi
cat $BATT_DIR/status > $OLD_STATUS


Cheers!