Giter Site home page Giter Site logo

batterylog's Introduction

Contents.

  1. What is this?
  2. Installation.
  3. Notes.

What is this?

This github repository contains a simple shell script that can be used to log the capacity of your laptop battery. It relies on data that is made available by Linux under /sys/class/power_supply/ and makes a timestamped copy of this data in a specified file every time the script is called (but only if the data has changed). Only data-logging, nothing else. But this gives quite interesting plots after a few years of running:

Plot of battery.log with gnuplot

A few things to note:

  • This plot contains data (capacity and level) for two batteries, alternating between the two.
  • The capacity decreases when the battery is used and less so when it is not being used (unsurprisingly).
  • The calibration (slope of the capacity curve as function of time) is very different for the two batteries.
  • The capacity of battery 2 increases abruptly after it is drained completely. This seems to have an impact on the calibration.
  • (Only visible in the higher-resolution version.) The capacity of battery 1 suddenly decreases when it is fully drained.

Installation.

Without any warrenties.

Step 1: Adjust path for log file.

First adjust the path to where the data should be written in the script battery_log.sh. It defaults to /tmp/battery.log and that file will be removed on every reboot, so your data will be lost. Therefore, look for the line

FILE_LOG=/tmp/battery.log # <- SPEFICY LOCATION FOR LOG FILE HERE

at the top of the script. Here you must give the absolute path to the output, i.e. replace /tmp/battery.log with a valid path to a (non-existing) file in your home directory.

Step 2: Setup cron job.

You only need to install the script to be run as a cron job: Run crontab -e and add the following line (replace [PATH] with the path to the script):

*/2  * * * * [PATH]/battery_log.sh cron >/tmp/batterylog.log

That's it! I.e. it will log data every 2 minutes when running (at most). (The redirect >/tmp/batterylog.log for the cronjob above is only for debugging and can be left out.)

Optional.

None of this is needed. Only if you want additional data points on start-up and shutdown, try the following.

Ubuntu 14.04

For Ubuntu 14.04 also install in rc etc.: In /etc/rc.local add line source [PATH]/battery_log.sh startup In /etc/rc0.d/K10batterymon add link to ../init.d/85batterymonshutdown In /etc/rc6.d/K10batterymon add link to ../init.d/85batterymonshutdown Create a file /etc/init.d/85batterymonshutdown with the following content (replace [PATH] with the path to the script):

#!/bin/bash
source [PATH]/battery_log.sh shutdown_initd

Create another file /etc/pm/sleep.d/20_alex with the following content (replace [PATH] with the path to the script):

#!/bin/bash
source [PATH]/battery_log.sh $1

Ubuntu 15.04+ (using systemd)

Create a file /etc/systemd/system/batterylog.service with the following content (replace [PATH] with the path to the script):

[Unit]
Description=Battery logging service

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=[PATH]/battery_log.sh startup_service
ExecStop=[PATH]/battery_log.sh shutdown_service

[Install]
WantedBy=multi-user.target

Run systemctl enable batterylog. Create another file /lib/systemd/system-sleep/battery_log with the following content (replace [PATH] with the path to the script):

#!/bin/bash
source [PATH]/battery_log.sh $1 $2

and make this executable (chmod +x ...).

Notes.

A line in ihe output log file might look like this:

1390222801 101 89480000 90720000 93240000 Discharging 0 10422000 12219000 45000 cron

The different columns mean the following:

  • Unix timestamp
  • serial number of the battery
  • current charge (in µWh)
  • charge when full (in µWh)
  • design charge when full (in µWh, will be constant for all lines)
  • status (Charging, Discharging, Full, Unknown)
  • AC connected (0, 1)
  • current power (in µW)
  • current voltage (in V)
  • current temperature
  • source of this line (cron for cron job)

The temperature is read from /sys/class/hwmon/hwmon0/temp1_input. This may not be the right sensor for your machine.

batterylog's People

Contributors

fuenfundachtzig avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.