Skip to content

Instantly share code, notes, and snippets.

@sethhall
Created April 29, 2014 14:09
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 sethhall/11401477 to your computer and use it in GitHub Desktop.
Save sethhall/11401477 to your computer and use it in GitHub Desktop.
cpu_governor
#!/bin/bash
available_governors=$(cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_available_governors \
| head -1 | sed -e 's/ \([a-zA-Z0-9]\)/|\1/g' -e 's/ $//')
if [ $# -ne 1 ]
then
echo "USAGE: $0 [$available_governors]"
fi
## CPU Governor path
#/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
function current_cpu_governor ()
{
echo -n "Current CPU Scaling Governor is: "
cpu_scaling_governor="NOT SET"
for governor in $(ls /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor)
do
cpu_scaling_governor=$(cat $governor)
done
echo "$cpu_scaling_governor"
}
current_cpu_governor;
## Exit, if no governor is provided
new_governor=""
if [ $# -eq 0 ]
then
exit 0
else
new_governor="$1"
fi
## Run as root always
user_id=`whoami`
if [[ "$user_id" != "root" ]]
then
echo "$0: please run this script as root user."
exit
fi
if [ -z $(echo $available_governors | sed -e 's/^/|/' -e 's/$/|/' | grep "|$new_governor|") ]
then
echo "Sorry, this mode '$new_governor' is not supported."
exit 1
else
echo "Setting CPU into '$new_governor' Mode..."
fi
## Now set cpu governor to the given mode
for governor in $(ls /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor)
do
echo "$new_governor" > $governor
done
current_cpu_governor;
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment