Skip to content

Instantly share code, notes, and snippets.

@peheje
Last active July 21, 2024 15:20
Show Gist options
  • Save peheje/36d73c0504440692b02bf8b0f2507986 to your computer and use it in GitHub Desktop.
Save peheje/36d73c0504440692b02bf8b0f2507986 to your computer and use it in GitHub Desktop.
linux auto-switch battery profile based on AC/Battery status
#!/bin/bash
get_power_source() {
if [ -f /sys/class/power_supply/ACAD/online ]; then
cat /sys/class/power_supply/ACAD/online
else
echo "Unable to determine power source"
exit 1
fi
}
set_power_mode() {
if [ "$1" == "1" ]; then
powerprofilesctl set balanced
else
powerprofilesctl set power-saver
fi
}
while true; do
power_source=$(get_power_source)
if [ "$power_source" == "1" ]; then
set_power_mode 1
elif [ "$power_source" == "0" ]; then
set_power_mode 0
fi
sleep 30
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment