From 9330d0c3dc644b6d891a536e62f4786b3afa224a Mon Sep 17 00:00:00 2001 From: Stefan Huber Date: Sun, 10 Nov 2024 16:04:05 +0100 Subject: [PATCH] systemd: Add batter script to system-sleep --- systemd/usr/lib/systemd/system-sleep/battery | 24 ++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100755 systemd/usr/lib/systemd/system-sleep/battery diff --git a/systemd/usr/lib/systemd/system-sleep/battery b/systemd/usr/lib/systemd/system-sleep/battery new file mode 100755 index 0000000..8730371 --- /dev/null +++ b/systemd/usr/lib/systemd/system-sleep/battery @@ -0,0 +1,24 @@ +#!/bin/sh +# Stefan Huber, 2024-11-10 + +acpi=$(acpi) +date=$(date -Iseconds --utc) +addendum="" + +if [ "$1" = "post" ]; then + lastpre=$(grep pre /var/log/batterstat.log | tail -n1) + + if [ ! -z "${lastpre}" ]; then + preprecent=$(echo "${lastpre}" | cut -f2 -d, | cut -f1 -d% | awk '{ print $1 }') + predate=$(echo "${lastpre}" | grep -o " at .*" | awk '{ print $2 }') + + postpercent=$(echo "${acpi}" | cut -f2 -d, | cut -f1 -d% | awk '{ print $1 }') + + let diffmin=($(date +%s -d ${date})-$(date +%s -d ${predate}))/60 + let diffperc=(prepercent-postprecent) + + addendum=", discharged ${diffperc}% (${preprecent}% -> ${postpercent}%) in ${diffmin} minutes" + fi +fi + +echo "$1: ${acpi} at ${date}${addendum}" >> /var/log/batterstat.log -- 2.39.5