mirror of
https://github.com/pigmonkey/spark.git
synced 2026-01-22 18:14:16 +00:00
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:
parent
65d52759b3
commit
35b94beb41
4 changed files with 88 additions and 2 deletions
6
roles/laptop/files/power_monitor.service
Normal file
6
roles/laptop/files/power_monitor.service
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[Service]
|
||||
Environment=STARTUP_WAIT=30s
|
||||
ExecStart=/usr/local/bin/power_monitor.sh
|
||||
|
||||
[Install]
|
||||
WantedBy=default.target
|
||||
43
roles/laptop/files/power_monitor.sh
Normal file
43
roles/laptop/files/power_monitor.sh
Normal 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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue