Skip to content

Instantly share code, notes, and snippets.

@redraw
Last active February 19, 2022 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save redraw/fc0602abd886ff8ef04bc54a0d443f9f to your computer and use it in GitHub Desktop.
Save redraw/fc0602abd886ff8ef04bc54a0d443f9f to your computer and use it in GitHub Desktop.
/usr/lib/systemd/system-sleep/battery-monitor
#!/bin/bash
# battery monitor systemd suspend/resume hook
LOGFILE=/var/log/battery.csv
KERNEL=$(uname -r)
BIOS=$(dmidecode -s bios-version)
log() {
cd /sys/class/power_supply
for power_supply in BAT*/; do
if [ -f `pwd`/$power_supply/energy_full ] && [ -f `pwd`/$power_supply/energy_now ]; then
ENERGY_FULL=`cat $power_supply/energy_full`
ENERGY_NOW=`cat $power_supply/energy_now`
TIME=`date "+%s"`
echo \"$BIOS\",\"$KERNEL\",\"$1\",\"$power_supply\",\"$ENERGY_FULL\",\"$ENERGY_NOW\",\"$TIME\" >> $LOGFILE
fi
done
}
case $1 in
pre) log $2 ;; # "suspend", "hibernate", "hybrid-sleep", or "suspend-then-hibernate"
post) log resume ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment