Skip to main content

Linux laptop battery suspend and resume logging tool

Project description

batterylog

Linux laptop battery logging tool

A simple Python app with few dependencies that reads your sysfs-class-power numbers and records them to a local sqlite3 db with an "event" tag.

It was built to track suspend power usage for Framework laptops, but it should work on other Linux laptops that expose battery data through /sys/class/power_supply.

Install Status

Native Python packaging is the recommended install path:

  • pipx install batterylog
  • uv tool install batterylog
  • pip install batterylog

INSTALL.sh is the legacy install format. It remains supported for existing installs and upgrades.

Existing legacy command behavior is part of the upgrade contract: batterylog.py suspend, batterylog.py resume, and the zero-argument report should keep working for upgraded legacy installs. Existing legacy data is also part of that contract: upgrades should not silently relocate or replace /opt/batterylog/batterylog.db. If schema upgrades are needed in the future, they should happen transparently in place for the active database rather than requiring manual intervention.

Quick Start

Install batterylog, then run:

batterylog

For persistent system hook installs from this repo, the legacy helper still works:

git clone https://github.com/lhl/batterylog.git
cd batterylog
./INSTALL.sh

Usage

The default invocation reports the most recent complete suspend/resume cycle:

batterylog

Recent-cycle views:

batterylog history --limit 10
batterylog summary --limit 10
batterylog history --discharging-only

Administrative commands:

batterylog --help
batterylog --version
sudo batterylog install-hook
sudo batterylog uninstall-hook
batterylog migrate-db --from /opt/batterylog/batterylog.db --to /var/lib/batterylog/batterylog.db

Charging sessions are reported as battery gain instead of negative usage, and suspend/resume rows now record charger-state context for later inspection.

For legacy /opt installs, replace batterylog with /opt/batterylog/batterylog.py.

Legacy Install

Make sure you meet the requirements, clone the repo, and run INSTALL.sh.

The legacy installer stages or refreshes /opt/batterylog, then runs the managed hook installer with the legacy DB path so logging continues to write to /opt/batterylog/batterylog.db.

Future installer work will keep this path functioning for upgrades and reinstalls, but new packaged releases should eventually be preferred over this legacy flow.

You can run /opt/batterylog/batterylog.py without any parameters and it will calculate the power usage from the last suspend/resume cycle:

$ /opt/batterylog/batterylog.py
Slept for 8.72 hours
Used 6.10 Wh, an average rate of 0.70 W
For your 53.67 Wh battery this is 1.30%/hr or 31.29%/day

This script looks for the first battery at /sys/class/power_supply/BAT*. It has currently only been tested with a Framework laptop, and some machines may not expose every value it expects. The tool is intentionally small and CLI-first: it stores the raw suspend/resume history in sqlite and exposes a few practical reporting commands on top.

The new history and summary commands cover the most common review use cases without adding a heavier UI layer.

Native Python Installs

Common install commands:

pipx install batterylog
uv tool install batterylog
pip install batterylog

Ephemeral CLI checks:

uvx batterylog --help

For a persistent system suspend hook from a repo checkout, INSTALL.sh is still the simplest documented setup in this project today.

Requirements

  • Linux with battery data exposed through /sys/class/power_supply
  • systemd
  • Python 3.10+
  • sqlite3 CLI is optional, but useful for manual inspection/debugging

Packaging

  • Arch Linux AUR: batterylog-git packaged by Stetsed
  • Reference AUR packaging for the current tree: packaging/aur/PKGBUILD

Other Related Tools

Other Related Python Libraries

Reference

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

batterylog-0.2.1.tar.gz (41.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

batterylog-0.2.1-py3-none-any.whl (28.8 kB view details)

Uploaded Python 3

File details

Details for the file batterylog-0.2.1.tar.gz.

File metadata

  • Download URL: batterylog-0.2.1.tar.gz
  • Upload date:
  • Size: 41.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.6

File hashes

Hashes for batterylog-0.2.1.tar.gz
Algorithm Hash digest
SHA256 3c56545612214b7a1717d6b92a7b0f87b63d8140014125b71bb3b7ced4c92cad
MD5 a86a5a814852a4279c74e49c685a18c7
BLAKE2b-256 78e9a9eca21b19a6973aa590bc82efcba261ed6d6117154d6f5431e5bc068d30

See more details on using hashes here.

File details

Details for the file batterylog-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: batterylog-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 28.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.6

File hashes

Hashes for batterylog-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 666e0c87907cfe591d64ff3e03ef255b7102a7295335a2ed1ecc991f8e5c4306
MD5 23e87740cde4c0c95a68491b4b1b7d49
BLAKE2b-256 433812b0adff2725e809dbc3f8649e1db5eebf9fde6715705e08a6310ad64852

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page