Skip to content

Instantly share code, notes, and snippets.

@lightcode
Created October 4, 2015 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lightcode/ba2915c4a50805c5cd8f to your computer and use it in GitHub Desktop.
Save lightcode/ba2915c4a50805c5cd8f to your computer and use it in GitHub Desktop.
Power saving script
#!/bin/bash
####
# Configuration
####
SOUND_POWERSAVE_BATTERY=1
SOUND_POWERSAVE_AC=0
WIFI_IFNAME="wlp4s0"
WIFI_POWERSAVE_BATTERY="on"
WIFI_POWERSAVE_AC="off"
# ondemand Dynamically switch between CPU(s) available if at 95% cpu load
# performance Run the cpu at max frequency
# conservative Dynamically switch between CPU(s) available if at 75% load
# powersave Run the cpu at the minimum frequency
# userspace Run the cpu at user specified frequencies
CPU_GOVERNOR_BATTERY=powersave
CPU_GOVERNOR_AC=ondemand
exec_all_session() {
local username="$1" ; shift
local cmd="$*"
source "/home/$username/.dbus/session-bus/$(</var/lib/dbus/machine-id)-0"
export DBUS_SESSION_BUS_ADDRESS
eval export $(grep -z ^DISPLAY /proc/$DBUS_SESSION_BUS_PID/environ)
su $username -c "$cmd"
}
####
# Configuration
####
case "$1" in
ac) mode="AC" ;;
battery) mode="BATTERY" ;;
*) _usage ;;
esac
_usage() {
echo "Usage: $0 [ac|battery]"
exit 1
}
_getvalue() {
eval "echo \$${1}_${mode}"
}
title="ACPI changed"
body="Energy change to $mode"
exec_all_session "matthieu" "notify-send \"$title\" \"$body\""
# SOUND
echo $(_getvalue SOUND_POWERSAVE) > /sys/module/snd_hda_intel/parameters/power_save
# CPU
echo $(_getvalue CPU_GOVERNOR) | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor >/dev/null
# WIFI
/usr/sbin/iw dev "$WIFI_IFNAME" set power_save "$(_getvalue WIFI_POWERSAVE)"
# Execute energy.sh when the power supply change its state
SUBSYSTEM=="power_supply", ATTR{online}=="0", RUN+="/opt/ansible-workstation/laptop-udev/scripts/energy.sh battery"
SUBSYSTEM=="power_supply", ATTR{online}=="1", RUN+="/opt/ansible-workstation/laptop-udev/scripts/energy.sh ac"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment