Skip to content

Instantly share code, notes, and snippets.

@vwbusguy
Last active March 8, 2024 21:27
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vwbusguy/02992061b3054e6b1cafe9b94bad500e to your computer and use it in GitHub Desktop.
Save vwbusguy/02992061b3054e6b1cafe9b94bad500e to your computer and use it in GitHub Desktop.
Auto Update for power-profiles-daemon
#!/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=$(powerprofilesctl list | grep -q performance && echo "performance" || echo "balanced")
elif [ $BATT_STAT -eq 5 ]; then
LEVEL="balanced"
else
LEVEL="power-saver"
fi
echo "Changing power level to ${LEVEL}"
gdbus call --system --dest net.hadess.PowerProfiles --object-path /net/hadess/PowerProfiles --method org.freedesktop.DBus.Properties.Set 'net.hadess.PowerProfiles' 'ActiveProfile' "<'${LEVEL}'>" > /dev/null
[[ $? -ne 0 ]] && echo "Could not change power level to ${LEVEL}!"
fi
done
@winstonrc
Copy link

Where do you place this script?

@vwbusguy
Copy link
Author

/usr/local/bin would be a good place, then have systemd call it from there via service. It logs to the system log that way.

@vwbusguy
Copy link
Author

vwbusguy commented Sep 12, 2023

Assuming the script is executable in /usr/local/bin, here's a working systemd service file for it:

[Unit]
Description=Automatically Adjust Power Profile
After=upower.service

[Service]
Type=simple
ExecStart=/usr/local/bin/auto_profile

[Install]
WantedBy=multi-user.target

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment