-
-
Save vwbusguy/02992061b3054e6b1cafe9b94bad500e to your computer and use it in GitHub Desktop.
#!/bin/bash | |
dbus-monitor --system "type='signal',path='/org/freedesktop/UPower/devices/battery_BAT0',member='PropertiesChanged'" | while read LINE; do | |
echo ${LINE} | grep battery_BAT0 | grep -q PropertiesChanged | |
if [ $? -eq 0 ]; then | |
BATT_STAT=$(dbus-send --print-reply=literal --system --dest=org.freedesktop.UPower /org/freedesktop/UPower/devices/battery_BAT0 org.freedesktop.DBus.Properties.Get string:org.freedesktop.UPower.Device string:State | awk '{ print $3; }') | |
if [ $BATT_STAT -eq 1 ] || [ $BATT_STAT -eq 4 ]; then | |
LEVEL=$(tuned-adm list | grep -q throughput-performance && echo "throughput-performance" || echo "balanced") | |
elif [ $BATT_STAT -eq 5 ]; then | |
LEVEL="balanced" | |
else | |
LEVEL="powersave" | |
fi | |
if [[ "$(tuned-adm active | sed 's,.*: ,,')" != "$LEVEL" ]] ; then | |
echo "Changing power level to ${LEVEL}" | |
tuned-adm profile $LEVEL | |
[[ $? -ne 0 ]] && echo "Could not change power level to ${LEVEL}!" | |
fi | |
fi | |
done |
Just for any new users, the path referred to by ExecStart should be the name of the bash file, so if you named it "auto-profile-update.sh" (as stated at the top), then it should be /usr/local/bin/auto-profile-update.sh
.
Also, make sure to give auto-profile-update.sh
(or whatever you've called it) executable permissions by running the command
chmod a+x <file-name-here.sh>
in /usr/local/bin
Thanks for that. I should note that this script is probably no longer useful For Fedora users as of Fedora 41 since Fedora has since moved to upower by default.
I just updated this to work for Fedora 41 via tuned. Use the previous version if you're on Fedora 40 or otherwise using the powerprofiles daemon. I also updated the name to match the systemd service example. It'll also spam the logs less since it won't output anything if the level isn't changing now.
Note that tuned also has a "recommended" and "auto_profile" mode already, so you might consider just using the existing tuned methods. If you specifically want these modes based on the battery/charging status, then this script should still be useful.
Assuming the script is executable in /usr/local/bin, here's a working systemd service file for it:
Put that in /etc/systemd/system/auto-power-profile.service then
sudo systemctl daemon-reload && sudo systemctl enable --now auto-power-profile
From there, logs for it should show up in journald whenever you have a power status change and it should persist automatically on reboots.