You are not logged in.

#1 2011-03-10 22:46:56

JulianNymark
Member
From: Norway
Registered: 2010-11-25
Posts: 18
Website

Shell script for cpu governor toggling [SOLVED]

I am fairly new to shell (bash) scripting, but i thought i would at least manage an if test, though i was proven wrong :/

I have been looking at this for way to long by now, and i am positive i am just missing the obvious, though I just cannot see where im wrong (checked alot of examples of similar bash scripts)

The problem is that the code runs as if the if statement is always true (as if the `cat /sys/.../scaling_governor` is never updated). So  what I think might have a say is the fact that it gathers info from /sys/... since i have never done that before

#!/bin/bash

if [ `cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor`="performance" ]
then
    echo "conservative" >/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
    echo "conservative" >/sys/devices/system/cpu/cpu1/cpufreq/scaling_governor
    echo "conservative" >/sys/devices/system/cpu/cpu2/cpufreq/scaling_governor
    echo "conservative" >/sys/devices/system/cpu/cpu3/cpufreq/scaling_governor
    echo "cpu governor: conservative"
else
    echo "performance" >/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
    echo "performance" >/sys/devices/system/cpu/cpu1/cpufreq/scaling_governor
    echo "performance" >/sys/devices/system/cpu/cpu2/cpufreq/scaling_governor
    echo "performance" >/sys/devices/system/cpu/cpu3/cpufreq/scaling_governor
    echo "cpu governor: performance"
fi

the code should "toggle" the governor for each core by checking 1 of them and applying it to all (I know the code probably looks sub-par, so if you can't handle it, please tell me how to improve it :P, or if there is a better solution to this whole thing altogheter)

Thanks in advance, and I will be trying to solve this in the mean time :)

Last edited by JulianNymark (2011-03-10 23:47:45)


When there is nothing left to do. Do nothing.

Offline

#2 2011-03-10 23:03:31

litemotiv
Forum Fellow
Registered: 2008-08-01
Posts: 5,026

Re: Shell script for cpu governor toggling [SOLVED]

How about using ondemand instead without any shell scripts?


ᶘ ᵒᴥᵒᶅ

Offline

#3 2011-03-10 23:23:40

JulianNymark
Member
From: Norway
Registered: 2010-11-25
Posts: 18
Website

Re: Shell script for cpu governor toggling [SOLVED]

True, I suppose I should just find 1 mode to fit all my needs. But I like to keep my laptop in as powersaving of a state as possible for 99% of the time, this script is what I want to bind to that spare blue key on my pc, for those times I must have it running smoothly. smile (ie. not strictly needed)

So this isn't too important to solve atm. Thus I might just stick to conservative (as I mainly use a text editor anyway) Though im not giving up on this yet. (I spent so much time already D:)


When there is nothing left to do. Do nothing.

Offline

#4 2011-03-10 23:25:47

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Shell script for cpu governor toggling [SOLVED]

You forgot to add spaces around =

As for improvements, tee can write to multiple files, and it also writes to stdout: (untested)

[[ $(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor) = performance ]] && new=conservative || new=performance
echo -n "cpu governor: "
echo $new | tee /sys/devices/system/cpu/*/cpufreq/scaling_governor

Offline

#5 2011-03-10 23:43:40

JulianNymark
Member
From: Norway
Registered: 2010-11-25
Posts: 18
Website

Re: Shell script for cpu governor toggling [SOLVED]

wow, that was a nice script yikes, 3 lines.

It works perfectly, and ive learned quite a bit of scripting from it! thank you! this made my day big_smile

and again thanks for such fast responses, awesome forum!


When there is nothing left to do. Do nothing.

Offline

#6 2011-03-11 08:52:44

admiralspark
Member
From: Alaska, USA
Registered: 2011-01-07
Posts: 87

Re: Shell script for cpu governor toggling [SOLVED]

tee makes bash scripting so much cleaner, it saved me alot of work when I wrote a scale-adjusting script for my i7!
One thing you might like, to make it even cleaner, instead of echo'ing to the file directly, you can use "cpufreq-set -g performance -r" switching performance with whichever governor you want. This would be more useful if you have your power management utilities controlling cpu speed as well. (-g sets governor, -r copies to every cpu core that is physically the same, aka in my i7 it makes all 8 go to performance)


Team Ignition Kernel Developer
linux-ideapad developer/maintainer
Flame Kernel developer for Galaxy Nexus and Galaxy S3
Want a cheap, reliable VPS with AWESOME customer service?

Offline

#7 2011-03-11 11:26:33

owain
Member
Registered: 2009-08-24
Posts: 251

Re: Shell script for cpu governor toggling [SOLVED]

I use the following one-liner to give a GUI selection, using zenity, and the relevant entries in /etc/sudoers:

sudo /usr/bin/cpufreq-set -r -g $(cpufreq-info -g | sed 's| |\n|g' | zenity --title 'cpufreq-set' --text "Current governor: "$(cpufreq-info -p | cut -d ' ' -f 3) --list --column "Choose governor:" 2>/dev/null)

Offline

#8 2011-03-11 16:44:58

JulianNymark
Member
From: Norway
Registered: 2010-11-25
Posts: 18
Website

Re: Shell script for cpu governor toggling [SOLVED]

That was a good idea AdmiralSpark (and also owain smile ), I didn't know that I could pass -r in cpufreq-set.
I really should be more thorough in my manual reading tongue

I don't think i will be going for a GUI selection (though that is a good idea for this).

The next (and final) step for me would be to have some sort of pop-up message display what governor i switched to. (since I bound this to a button press event it will not display in active terminal?). But I havent really started looking into that yet, and should probably make another thread for that after i fail miserably tongue.

Thanks again for the help & suggestions.


When there is nothing left to do. Do nothing.

Offline

#9 2011-03-11 16:51:31

skunktrader
Member
From: Brisbane, Australia
Registered: 2010-02-14
Posts: 1,543

Re: Shell script for cpu governor toggling [SOLVED]

JulianNymark wrote:

The next (and final) step for me would be to have some sort of pop-up message display what governor i switched to. (since I bound this to a button press event it will not display in active terminal?). But I havent really started looking into that yet, and should probably make another thread for that after i fail miserably tongue.

notify-send -t 10000 "my title" "my message"

Offline

#10 2011-03-11 17:04:39

JulianNymark
Member
From: Norway
Registered: 2010-11-25
Posts: 18
Website

Re: Shell script for cpu governor toggling [SOLVED]

Perfect! big_smile

Everything is now the way I want it.

yet again, thanks! smile


When there is nothing left to do. Do nothing.

Offline

#11 2011-03-11 17:05:21

JulianNymark
Member
From: Norway
Registered: 2010-11-25
Posts: 18
Website

Re: Shell script for cpu governor toggling [SOLVED]

EDIT: this message was a duplicate (managed to resend tongue)

Last edited by JulianNymark (2011-03-11 17:06:27)


When there is nothing left to do. Do nothing.

Offline

Board footer

Powered by FluxBB