2

Bit of a strange one. I have a python file that is activated by an AppleScript. My python script does a couple of basic commands. One thing it does is try and run a sudo command in a terminal window. My problem is that a sudo command needs a password to be executed. I can't figure out how to enter my password in the python file, as the code wants a password input for in order to execute the sudo command.

The apple script below calls a python script known as setting_pm.py to be run from my desktop folder.

Apple script:

set desktop_folder to "$HOME/Desktop"
tell application "Terminal"
    activate
    do script "cd desktop;cd setting_pm;python3 setting_pm.py"
end tell

Python file:

os.system("sudo pmset schedule cancelall")
os.system("myPassword")

If you test the code above you will see that the password gets asked for but it wont seem to accept the next command where i try and input my password.

I have played around with removing passwords from sudo but i like the idea of having passwords enabled, and only entering the password in this specific situation from a python script.

2
  • 1
    Does this answer your question? Using sudo with Python script Commented Aug 31, 2021 at 20:54
  • had a look there but it didn't seem to do it. thanks though. i got it: Commented Aug 31, 2021 at 21:17

1 Answer 1

3

answer:

import os
os.system("echo password | sudo -S pmset schedule cancelall")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.