auto-switch tuned profiles on battery

I'm not sure if a looping script like this is the best way to accomplish
this. I don't understand why TuneD doesn't provide this functionality
itself, like TLP does.
This commit is contained in:
Pig Monkey 2025-01-14 08:19:55 -08:00
parent 65d52759b3
commit 35b94beb41
4 changed files with 88 additions and 2 deletions

View file

@ -0,0 +1,6 @@
[Service]
Environment=STARTUP_WAIT=30s
ExecStart=/usr/local/bin/power_monitor.sh
[Install]
WantedBy=default.target

View file

@ -0,0 +1,43 @@
#! /bin/bash
# Original stolen from Kobus van Schoor:
# https://kobusvs.co.za/blog/power-profile-switching/
BAT=$(echo /sys/class/power_supply/BAT*)
BAT_STATUS="$BAT/status"
BAT_CAP="$BAT/capacity"
LOW_BAT_PERCENT=50
AC_PROFILE="desktop"
BAT_PROFILE="balanced"
LOW_BAT_PROFILE="laptop-battery-powersave"
# wait a while if needed
[[ -z $STARTUP_WAIT ]] || sleep "$STARTUP_WAIT"
# start the monitor loop
prev=0
while true; do
# read the current state
if [[ $(cat "$BAT_STATUS") == "Discharging" ]]; then
if [[ $(cat "$BAT_CAP") -gt $LOW_BAT_PERCENT ]]; then
profile=$BAT_PROFILE
else
profile=$LOW_BAT_PROFILE
fi
else
profile=$AC_PROFILE
fi
# set the new profile
if [[ $prev != "$profile" ]]; then
echo setting power profile to $profile
# powerprofilesctl set $profile
tuned-adm profile $profile
fi
prev=$profile
# wait for the next power change event
inotifywait -qq "$BAT_STATUS" "$BAT_CAP"
done

View file

@ -2,5 +2,7 @@
- name: restart tlp
service: name=tlp.service state=restarted
- name: restart lowbatt
service: name=lowbatt.timer state=restarted
- name: restart power monitor
service:
name: power_monitor.service
state: restarted

View file

@ -13,3 +13,38 @@
state: started
tags:
- tuned
- name: Install inotify-tools
pacman:
name: inotify-tools
state: present
tags:
- tuned
- name: Install power monitor script
copy:
src: power_monitor.sh
dest: /usr/local/bin/
mode: 0755
notify:
- restart power monitor
tags:
- tuned
- name: Copy power monitor service
copy:
src: power_monitor.service
dest: /etc/systemd/system/
notify:
- reload systemd config
- restart power monitor
tags:
- tuned
- name: Enable and start power monitor service
service:
name: power_monitor.service
enabled: yes
state: started
tags:
- tuned