Skip to content

Instantly share code, notes, and snippets.

@dblume
Last active May 12, 2024 06:39
Show Gist options
  • Save dblume/c41357604abf92f7fc1ceda9da2f7f92 to your computer and use it in GitHub Desktop.
Save dblume/c41357604abf92f7fc1ceda9da2f7f92 to your computer and use it in GitHub Desktop.
Log to file the current state of the battery to track trends
#!/usr/bin/env bash
#
# A script I use to track the health of my FrameWork laptop battery
#
# Usage Ex.,
#
# $ logpower closing lid
#
# will write to stdout and to a file:
# 1. Timestamp
# 2. Battery percentage
# 3. Battery state (discharging, charging)
# 4. time to full or empty
# 5. Any additional notes
#
# 2024-05-11T14:45:44-07:00 87%, discharging, time to empty: 12.2 hours, "closing lid"
#
set -euf -o pipefail
# Just append stdout to the file
#exec >>~/Documents/battery_status_notes.txt
# Write to stdout and append to the file
exec > >(tee -a ~/Documents/battery_status_notes.txt)
printf "%s%s, \"%s\"\n" \
"$(date -Iseconds)" \
"$(upower -i $(upower -e | grep 'BAT') | \
grep -E "state|to\ full|to\ empty|percentage" | tr '\n' ',' | tr -s ' ' | \
awk -F ',' 'BEGIN{OFS=FS}{print substr($3,13),substr($1,8),$2}')" \
"$(echo -n "$@")"
# Alternatively, use logger to send it to /var/log/syslog with a $HOSTNAME $USER prefix.
#logger "$(upower -i $(upower -e | grep 'BAT') | \
# grep -E "state|to\ full|to\ empty|percentage" | tr '\n' ',' | tr -s ' ' | \
# awk -F ',' 'BEGIN{OFS=FS}{print substr($3,13),substr($1,8),$2,sep}')" \
# "$(echo -n \""$@"\")"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment