]> git.sthu.org Git - shutils.git/commitdiff
systemd: Add batter script to system-sleep
authorStefan Huber <shuber@sthu.org>
Sun, 10 Nov 2024 15:04:05 +0000 (16:04 +0100)
committerStefan Huber <shuber@sthu.org>
Sun, 10 Nov 2024 15:04:05 +0000 (16:04 +0100)
systemd/usr/lib/systemd/system-sleep/battery [new file with mode: 0755]

diff --git a/systemd/usr/lib/systemd/system-sleep/battery b/systemd/usr/lib/systemd/system-sleep/battery
new file mode 100755 (executable)
index 0000000..8730371
--- /dev/null
@@ -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