Notebook Battery Monitor
Enable Javascript to display Table of Contents.
Script
The following script will monitor the battery load before and after suspending the system, and
whenever it is called - e.g. by a cronjob every 5 minutes.
#!/bin/sh
#
# Monitor system sleep/suspend/hibernate
#
NAME=$(basename $0)
FULL=$(cat /sys/class/power_supply/BAT0/charge_full)
NOW=$(cat /sys/class/power_supply/BAT0/charge_now)
ACTION=$(cat /sys/class/power_supply/BAT0/status)
PERCENT=$((($NOW*100+$FULL/2)/$FULL))
case $1 in
pre)
echo "Going to $2: $PERCENT% ($ACTION $NOW of $FULL)" | systemd-cat -t $NAME -p info
;;
post)
echo "Resuming from $2: $PERCENT% ($ACTION $NOW of $FULL)" | systemd-cat -t $NAME -p info
;;
log)
echo "Logging: $PERCENT% ($ACTION $NOW of $FULL)" | systemd-cat -t $NAME -p info
;;
print)
journalctl --identifier $NAME --boot
;;
print-all)
journalctl --identifier $NAME
;;
esac
Execution on Suspend/Resume
With saving the script to /usr/lib/systemd/system-sleep/monitor_sleep
, the code is called with parameter pre
and post
on suspend or resume.
Execution via Cronjob
The cronjob is executed ever 5 minutes:
21:46:26[~]$ sudo crontab -l
# m h dom mon dow command
*/15 * * * * /usr/sbin/powertop --auto-tune >/dev/null 2>&1
*/5 * * * * /usr/lib/systemd/system-sleep/monitor_sleep log
21:46:27[~]$
Output
The script will query journalctl
for getting the logs:
21:54:14[~]$ /usr/lib/systemd/system-sleep/monitor_sleep print
Nov 24 20:00:01 XPS-17-9700 monitor_sleep[4183]: Logging: 59% (Discharging 4594000 of 7838000)
Nov 24 20:15:01 XPS-17-9700 monitor_sleep[4893]: Logging: 53% (Discharging 4133000 of 7838000)
Nov 24 20:30:01 XPS-17-9700 monitor_sleep[5306]: Logging: 48% (Discharging 3724000 of 7838000)
Nov 24 20:45:01 XPS-17-9700 monitor_sleep[5926]: Logging: 42% (Discharging 3274000 of 7838000)
Nov 24 21:00:01 XPS-17-9700 monitor_sleep[6369]: Logging: 36% (Discharging 2788000 of 7838000)
Nov 24 21:11:29 XPS-17-9700 monitor_sleep[6627]: Going to hybrid-sleep: 3% (Discharging 218000 of 7838000)
Nov 24 21:12:17 XPS-17-9700 monitor_sleep[6850]: Resuming from hybrid-sleep: 2% (Charging 195000 of 7838000)
Nov 24 21:15:01 XPS-17-9700 monitor_sleep[7386]: Logging: 4% (Charging 348000 of 7838000)
Nov 24 21:20:01 XPS-17-9700 monitor_sleep[8392]: Logging: 8% (Charging 656000 of 7838000)
Nov 24 21:25:01 XPS-17-9700 monitor_sleep[9078]: Logging: 12% (Charging 964000 of 7838000)
Nov 24 21:30:01 XPS-17-9700 monitor_sleep[9246]: Logging: 16% (Charging 1271000 of 7838000)
Nov 24 21:35:01 XPS-17-9700 monitor_sleep[9290]: Logging: 20% (Charging 1579000 of 7838000)
Nov 24 21:40:01 XPS-17-9700 monitor_sleep[9403]: Logging: 24% (Charging 1889000 of 7838000)
Nov 24 21:45:01 XPS-17-9700 monitor_sleep[9486]: Logging: 28% (Charging 2197000 of 7838000)
Nov 24 21:50:01 XPS-17-9700 monitor_sleep[9640]: Logging: 32% (Charging 2503000 of 7838000)
21:54:33[~]$