You are on page 1of 2

Share Report Abuse Next Blog

Create Blog Sign In

p
sT
n
iux
Li
Useful Tips for GNU/Linux Operating System

Blog Archive
2011 (5)

Tuesday, May 24, 2011

CPe
Fq
U
rn
ug
eiS
nca
yl

June (1)
May (1)

CPU Frequency Scaling


March (2)
February (1)
2010 (1)
2009 (29)
2008 (43)

I have written a small shell script to increase or decrease CPU frequency. By default, it shows current CPU
Frequency Scaling Governor. This can be changed only by root user. So this script needs to be run as root
user or sudo as root, while changing the CPU Frequency Scaling governor.

#!/bin/bash

About Me
mitesh.singh.jat

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

Hello, This is Mitesh Singh Jat fromBangalore, India.


This blog is created as a small service to GNU/Linux
Community. The blog provides small and useful tips
for GNU/Linux, which I have learnt in due course of
using Linux. I will feel obliged if this blog is of any
use to anyone. Kindly write comment, if you have
any suggestions for this blog. Regards, Mitesh
View my complete profile

echo "Command line to change CPU Scaling."


echo "
- By Mitesh Singh Jat"
echo ""

## 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
) ]
then
echo
exit
else
echo
fi

$(echo $available_governors | sed -e 's/^/|/' -e 's/$/|/' | grep "|$new_governor|"


"Sorry, this mode '$new_governor' is not supported."
1
"Setting CPU into '$new_governor' Mode..."

## 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

Sample Run
Getting current CPU Frequency Scaling Governor
$ cd /path/where/cpu_scaling.sh/is/copied/

converted by Web2PDFConvert.com

$ ./cpu_scaling.sh
USAGE: ./cpu_scaling.sh [powersave|conservative|ondemand|userspace|performance]
Command line to change CPU Scaling.
- By Mitesh Singh Jat
Current CPU Scaling Governor is: ondemand
Increasing CPU frequency (Please run as root).
$ ./cpu_scaling.sh performance
Command line to change CPU Scaling.
- By Mitesh Singh Jat
Current CPU Scaling Governor is: ondemand
./cpu_scaling.sh: please run this script as root user.
$ sudo ./cpu_scaling.sh performance
[sudo] password for mitesh:
Command line to change CPU Scaling.
- By Mitesh Singh Jat
Current CPU Scaling Governor is: ondemand
Setting CPU into Performance Mode...
Current CPU Scaling Governor is: performance
$ ./cpu_scaling.sh
USAGE: ./cpu_scaling.sh [powersave|conservative|ondemand|userspace|performance]
Command line to change CPU Scaling.
- By Mitesh Singh Jat
Current CPU Scaling Governor is: performance
Decreasing CPU frequency
$ sudo ./cpu_scaling.sh ondemand
Command line to change CPU Scaling.
- By Mitesh Singh Jat
Current CPU Scaling Governor is: performance
Setting CPU into OnDemand Mode...
Current CPU Scaling Governor is: ondemand

Posted by mitesh.singh.jat at 2:11 PM


Labels: bash, cpu, linux, shell

0 comments:
Post a Comment

Links to this post


Create a Link

Newer
Post

Home

Older
Post

Subscribe to: Post Comments (Atom)

converted by Web2PDFConvert.com