You are not logged in.

#1 2015-01-26 21:46:15

gilmoreja
Member
From: Florida
Registered: 2012-05-28
Posts: 74

New to bash scripting; making script to change laptop CPU governor

I've got an old laptop and I like to be able to change the governor between performance and ondemand depending on whether or not I've got the laptop plugged in. I'd really like to be able to do this from a shortcut on the desktop, so I decided to try making a bash script that used Zenity dialogs to accomplish this.

I think it turned out pretty much how I'd like, however the error handling on getting the root password authenticated is pretty broken. I'm not sure how exactly I should go about it.. I think that the long timeout that occurs when you enter a wrong password for su is causing problems. Is there a better way of doing this? I don't use sudo, and I'd really prefer to avoid it and stick to using su if at all possible.

#!/bin/bash

#This script will supply a Zenity dialog with the option to change the laptop's governor.
#It will use a Zenity dialog to acquire the root password from user for autorization to change the governor.

#Set up some variables we will use.

optionPerformance="FALSE"
optionOndemand="FALSE"

#Read the current state of the governor.

currentGovernor=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)

#Change the variables we previously set up to reflect current state of governor. This will be used to determine which option is pre-selected in the Zenity dialog we will create.

if [ $currentGovernor = 'performance' ]
	then optionPerformance="TRUE"
fi

if [ $currentGovernor = 'ondemand' ]
	then opitonOndemand="TRUE"
fi

#Set up Zenity dialog which allows user to select the governor they want active. The currently active governor will be selected by default.
#User's input will go to the wantedGovernor variable for future use.

wantedGovernor=$(zenity --list --text "Select which governor you want to make active:" --radiolist --column "" --column "Options" $optionPerformance "performance" $optionOndemand "ondemand")

#Check to see if user clicked Cancel button.

if [ $? = 1 ]
	then exit 0
fi

#Did user select the already active profile? If so, simply report that and exit.

if [ $wantedGovernor = $currentGovernor ]
	then
		newGovernor=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)
		zenity --info --text "Governor is already set to $newGovernor.\n\nNo changes were made."
		exit 0
fi

#Now that we know what the user wants, let's do it.
#We will use cpupower to change the governor. This will require root privileges, so we will use Zenity to get the root password from user and pipe it into su. (Is this the best way?)

zenity --title "Password for root:" --password | su -c "cpupower frequency-set -g $wantedGovernor"

#Check to see if user clicked Cancel button.

if [ ${PIPESTATUS[0]} = 1 ]
	then exit 0
fi

#Check to see if user entered incorrect root password.

if [ ${PIPESTATUS[1]} = 1 ]
	then
		zenity --error --text "Incorrect root password.";
		exit 1
fi

#Now that we've made the change, let's check the current state of the governor again so that we can report it to the user and they can verify success.

newGovernor=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)

zenity --info --text "Governor is now set to $newGovernor."

Last edited by gilmoreja (2015-01-26 21:49:19)


Time you enjoy wasting isn't wasted time.

Offline

#2 2015-01-27 02:16:21

cynicalpsycho
Member
Registered: 2009-12-22
Posts: 57

Re: New to bash scripting; making script to change laptop CPU governor

Just out of curiosity,  what is it about  sudo you don't like?

Offline

#3 2015-01-27 09:39:45

x33a
Forum Fellow
Registered: 2009-08-15
Posts: 4,587

Re: New to bash scripting; making script to change laptop CPU governor

Maybe give gksu a try.

Offline

#4 2015-01-27 09:53:52

Awebb
Member
Registered: 2010-05-06
Posts: 6,286

Re: New to bash scripting; making script to change laptop CPU governor

Offline

#5 2015-01-27 15:41:14

gilmoreja
Member
From: Florida
Registered: 2012-05-28
Posts: 74

Re: New to bash scripting; making script to change laptop CPU governor

@cynicalpsycho
Simply because I don't want my username and password being able to do anything that requires root access. My computer is in a household environment and it is frequently used by other individuals. I would prefer to keep any privileged commands firmly behind the root login, therefore sudo isn't much use for me.

@Awebb
Thanks for linking that; I've seen it before, but it's not relevant to my setup. My laptop is using a 9-year-old, single-core, AMD Athlon 64.

@x33a
gksu may actually be what I'm looking for. Thanks for the advice, I'll see what I can do with it.


Time you enjoy wasting isn't wasted time.

Offline

#6 2015-01-27 16:53:38

gilmoreja
Member
From: Florida
Registered: 2012-05-28
Posts: 74

Re: New to bash scripting; making script to change laptop CPU governor

Thanks for the advice, Awebb. I looked into gksu but I didn't like how it offered to save the root password in gnome-keyring. I found an alternative, ktsuss, and decided to go with that instead.

I'll post the finished script here, for anyone who is ever interested in such a thing:

#!/bin/bash

#This script will supply a Zenity dialog with the option to change the laptop's governor.
#It will use a Zenity dialog to acquire the root password from user for autorization to change the governor.

#Set up some variables we will use.

optionPerformance="FALSE"
optionOndemand="FALSE"

#Read the current state of the governor.

currentGovernor=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)

#Change the variables we previously set up to reflect current state of governor. This will be used to determine which option is pre-selected in the Zenity dialog we will create.

if [ $currentGovernor = 'performance' ]
	then
		optionPerformance="TRUE"
fi

if [ $currentGovernor = 'ondemand' ]
	then optionOndemand="TRUE"
fi

#Set up Zenity dialog which allows user to select the governor they want active. The currently active governor will be selected by default.
#User's input will go to the wantedGovernor variable for future use.

wantedGovernor=$(zenity --list --text "Select which governor you want to make active:" --radiolist --column "" --column "Options" $optionPerformance "performance" $optionOndemand "ondemand")

#Check to see if user clicked Cancel button.

if [ $? = 1 ]
	then exit 0
fi

#Did user select the already active profile? If so, simply report that and exit.

if [ $wantedGovernor = $currentGovernor ]
	then
		newGovernor=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)
		zenity --info --text "Governor is already set to $newGovernor.\n\nNo changes were made."
		exit 0
fi

#Now that we know what the user wants, let's do it.
#We will use cpupower to change the governor. This will require root privileges, so we will use ktsuss to get that.

ktsuss cpupower frequency-set -g $wantedGovernor

#Now that we've made the change, let's check the current state of the governor again so that we can report it to the user and they can verify success.

newGovernor=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)

zenity --info --text "Governor is now set to $newGovernor."

Time you enjoy wasting isn't wasted time.

Offline

#7 2015-01-27 17:20:47

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: New to bash scripting; making script to change laptop CPU governor

If you are using bash (as your shebang states) you should consider:


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#8 2015-01-27 20:16:55

gilmoreja
Member
From: Florida
Registered: 2012-05-28
Posts: 74

Re: New to bash scripting; making script to change laptop CPU governor

Thanks for the links; they were informative. I cleaned up the code a bit to reflect the issues you pointed out.

I think the bit about "sparing the feline" was particularly interesting. I regularly think about using `>` to write into a file, but never once did it occur to me that using `<` would read from a file.

#!/bin/bash

#This script will supply a Zenity dialog with the option to change the laptop's governor.
#It will use a Zenity dialog to acquire the root password from user for autorization to change the governor.

#Set up some variables we will use.

optionPerformance=FALSE
optionOndemand=FALSE

#Read the current state of the governor.

currentGovernor=$(</sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)

#Change the variables we previously set up to reflect current state of governor. This will be used to determine which option is pre-selected in the Zenity dialog we will create.

if [[ $currentGovernor == performance ]]
	then
		optionPerformance=TRUE
fi

if [[ $currentGovernor == ondemand ]]
	then
		optionOndemand=TRUE
fi

#Set up Zenity dialog which allows user to select the governor they want active. The currently active governor will be selected by default.
#User's input will go to the wantedGovernor variable for future use.

wantedGovernor=$(zenity --list --text "Select which governor you want to make active:" --radiolist --column "" --column "Options" $optionPerformance "performance" $optionOndemand "ondemand")

#Check to see if user clicked Cancel button.

if [[ $? == 1 ]]
	then exit 0
fi

#Did user select the already active profile? If so, simply report that and exit.

if [[ $wantedGovernor == $currentGovernor ]]
	then
		newGovernor=$(</sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)
		zenity --info --text "Governor is already set to $newGovernor.\n\nNo changes were made."
		exit 0
fi

#Now that we know what the user wants, let's do it.
#We will use cpupower to change the governor. This will require root privileges, so we will use ktsuss to get that.

ktsuss cpupower frequency-set -g $wantedGovernor

#Now that we've made the change, let's check the current state of the governor again so that we can report it to the user and they can verify success.

newGovernor=$(</sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)

zenity --info --text "Governor is now set to $newGovernor."

Time you enjoy wasting isn't wasted time.

Offline

#9 2015-01-27 20:37:44

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: New to bash scripting; making script to change laptop CPU governor

gilmoreja wrote:

@cynicalpsycho
Simply because I don't want my username and password being able to do anything that requires root access. My computer is in a household environment and it is frequently used by other individuals. I would prefer to keep any privileged commands firmly behind the root login, therefore sudo isn't much use for me.

FYI, you can have sudo prompt for root's password instead of your user's password.


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

Board footer

Powered by FluxBB