Skip to content

Instantly share code, notes, and snippets.

@VladSavitsky
Forked from kobus-v-schoor/power_monitor.sh
Last active June 10, 2023 18:22
Show Gist options
  • Save VladSavitsky/807987cf468389811069260de967ff1c to your computer and use it in GitHub Desktop.
Save VladSavitsky/807987cf468389811069260de967ff1c to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
BAT=$(echo /sys/class/power_supply/BAT*)
BAT_STATUS="$BAT/status"
BAT_CAP="$BAT/capacity"
LOW_BAT_PERCENT=20
AC_PROFILE="performance"
BAT_PROFILE="balanced"
LOW_BAT_PROFILE="power-saver"
# 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
fi
prev=$profile
# wait for the next power change event
inotifywait -qq "$BAT_STATUS" "$BAT_CAP"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment