How to Run The `sudo` Command in KM?

Better is to use this full format explicit way:

your_short_username ALL= NOPASSWD: /usr/bin/pmset -a disksleep 10, !/usr/bin/pmset -a disksleep 10 *

That allows the command, and only the command, to be executed without a password.

Much better than allowing any pmset without a password.


Perhaps I misunderstand. I just tried this:

noisyneilALL= NOPASSWD: /usr/bin/pmset -a disksleep 10, !/usr/bin/pmset -a disksleep 10 *

and got...


Just follow the pattern shown in post #15: insert a space before ALL and remove the space after the equals sign… that’s my guess!

1 Like

Yes, I never nail these down hard enough. Thanks for the reminder, @peternlewis!

That command should be put into the pmset file created with visudo, replacing the line from before. It's basically replacing my "let me do everything with pmset without asking for a password" with "let me do this specific thing with pmset without asking for a password".

As @peternlewis said -- much better practice to allow only what is necessary, and no more.


Ohhhhhh sorry, I get it now.

Trying to use this to automate changes to scheduled wake times by running this shell script after setting the variable wakeTime with a prompt–

sudo pmset repeat wake MTWThFSSu %Variable%wakeTime%:00

I have so far unsuccessfully tried these variations of Peter's solution:

my_username ALL= NOPASSWD: /usr/bin/pmset -a wake 10, !/usr/bin/pmset -a wake 10 *

my_username ALL= NOPASSWD: /usr/bin/pmset -a repeat wake 10, !/usr/bin/pmset -a repeat wake 10 *

my_username ALL= NOPASSWD: /usr/bin/pmset -a pmset repeat wake 10, !/usr/bin/pmset -a pmset repeat wake 10 *

my_username ALL= NOPASSWD: /usr/bin/pmset -a sudo repeat wake 10, !/usr/bin/pmset -a sudo repeat wake 10 *

my_username ALL= NOPASSWD: /usr/bin/pmset -a sudo pmset repeat wake 10, !/usr/bin/pmset -a sudo pmset repeat wake 10 *

I then press esc, :wq, return, and try to run the shell script, getting this error:

Execute a Shell Script failed with script error: sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper
sudo: a password is required. Macro “Trying” cancelled (while executing Set schedule).

I know virtually nothing about using the terminal etc. This is my first time making a macro with a shell script. So, noob questions-

  • Is it the very top line of the sudo visudo file that I insert this? i.e., above all the lines that start with "# Sample /etc/sudoers file.”?
  • Is a restart required for changes to take effect?

@peternlewis If you happen to have time, do you know what the correct version of your solution would look like for this function?

Thanks for reading!

I've found this macro written by @ccstone to be very helpful to do sudo commands that require a password.

1 Like

Hi cyoungers!
I appreciate the thought, but the typing the password part scares me.
Good to know it's an option though. Cheers

It sounds like you want to allow sudo pmset repeat wake MTWThFSSu (anything) in which case I think you just want:

my_username ALL= NOPASSWD: /usr/bin/pmset repeat wake MTWThFSSu

I think. But I am not an expert.

1 Like

Thank you so much Peter! That got me very close and it was just a couple adjustments from there and now it all works perfectly.

For anyone's future reference, what ended up working was:

my_username ALL=(ALL) NOPASSWD: /usr/bin/pmset repeat wake *

Also, chatGPT suggested I had been putting it in the wrong place in the sudoers file, so I moved it from the top line to the bottom line. Once I did that, I stopped getting the 'needs password' error and started getting a 'badly formatted power command' error. I eventually realized thatI shouldn't have been putting token text in a shell script action, so looked it up on the KM wiki and quickly found the answer was to change it to:

sudo pmset repeat wake MTWThFSSu $KMVAR_wakeTime:00

1 Like