Posts Tagged ‘coding’

Monitor battery discharge in Linux

25 August 2013

I wondered how long the battery of my Acer Aspire One A110-BW would make me ‘socket-less’. Monitoring by hand is not very practical, so I started to bash around and did some low level coding.

The result is a simple script that monitors the discharging of a laptop battery. It starts with reporting basic info on the condition of the battery. Every 60 seconds the power status is written to the same file. The file and its lines have detailed date and time stamps to compare subsequent reports.

The bash script needs acpi installed, it should be in the repo of any Linux distro. You can run it as a regular user. Needless to say that the laptop should run on battery power…

[user@mybox ~]$ ( date && acpi -V | grep Battery ) > `date +%y%m%d_%Hh%M`_batterylog.txt; sleep 60; while x=0; do (( date && acpi -V | grep Discharging) ; sleep 60 ); done >> `date +%y%m%d_%Hh%M`_batterylog.txt

[changed the %H:M to %Hh%M in the output filenameĀ as the colon ‘:’ frustrates filemanagers and other CLI handling]

Bash spreads the output of two subsequent commands (here <date && acpi>) over two lines. I tried <echo -n> to make one line every minute but with no succes yet–it’s something I have to work on. I set monitoring pace to one minute (60 secs) to get detailed reports.

Use this script if you like. If you make interesting improvements I’d like to hear from you!