You are not logged in.
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
Just out of curiosity, what is it about sudo you don't like?
Offline
Maybe give gksu a try.
Offline
Offline
@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
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
If you are using bash (as your shebang states) you should consider:
`currentGovernor=$(</sys/devices...)`: sparing the feline
and being much more particular about quoting...
Offline
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
@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