How to Run The `sudo` Command in KM?

No joy with that either, I'm afraid.

KK...

So it works with -u when I'm "getting" from pmset, but when "setting" it whines because I'm not root. I'll have more of a poke later, or perhaps the smart kids will arrive and solve the problem.

If you really want to make this happen and are happy to echo your password, the following should work -- although you might have to get creative with character escaping if your password has anything funky in it (the single quotes should take care of most):

echo 'mysecretpassword' | sudo -S pmset -a disksleep 10
1 Like

Ok that works great! I have no problem echoing the password; I mean, if you've managed to log into my mac and thought to dig around in my KM macros, you've earned it!

Any reason to undo the sudo visudo? Might as well leave it, right?

Oh, FFS... I'm such an idiot (and autocorrect does my head in!)

Check the sudoers file created earlier and make sure that the path is right:

username ALL=NOPASSWD: /usr/bin/pmset

Note that the "e" in "user" shouldn't be there...

1 Like

Nailed it! Yep, works now! Thanks @Nige_S, you're a hero. :trophy:

I realise I semi-hijacked this thread, but I do think this has added at least one full solution to the topic.

Right, let's hope this actually stops my mac from waking in the middle of the night.

The above works, but I just tried to apply the same logic to the below, with no luck. Any reason you can think of?

echo 'my secret password' | sudo spctl --master-disable

The missing -S after sudo? That's the flag that says "get the password from standard input (the pipe in this case) rather than a terminal".

1 Like

To quote you...

:man_facepalming:t2:

1 Like

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.

2 Likes

Perhaps I misunderstand. I just tried this:

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

and got...

image

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.

2 Likes

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