0

Is it a good idea to put a sudo command in a shell configuration file?

I have a bash script /home/t/bin/changeCpuFreq.sh which can change my cpu frequency. The line 40 in my script is

echo $1 >  /sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed

Which determines that running the script requires root privillege

I would like the script to run automatically every time I reboot Ubuntu or log in, and wonder where to put the command to run the script?

  1. In ~/.profile, I put

    sudo /home/t/bin/changeCpuFreq.sh 1600000
    

    When I reboot Ubuntu, after logging in, Ubuntu reports an error

    Error found when loading /home/t/profile.

    sudo: no tty present and no askpass program specified.

    As a result the session will not be configured correctly.

  2. In /etc/profile, I put the following at the end:

    /home/t/bin/changeCpuFreq.sh 1600000 
    

    When I reboot Ubuntu, I have an error before logging in:

    Error found when loading /etc/profile:

    /home/t/bin/changeCpuFreq.sh: line 40

    /sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed: Permission denied

    As a result the session will not be configured correctly.

    Is it because I didn't set sudo up, and how shall I use sudo for a command in /etc/profile?

Thanks.

  • 2
    /etc/profile is never the right place to run a system-wide command on startup. – Ignacio Vazquez-Abrams Mar 31 '18 at 22:23
  • Thanks. I also tried ~/.profile, and added sudo, but got the sudo: no tty present and no askpass program specified error. I am confused. – Tim Mar 31 '18 at 22:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.