ENVIRONMENT:
Mac OS Catalina 10.15.7
Python 3.10
PROBLEM:
I am trying to execute a shell script from my python script in Mac OS X Catalina 10.15.
Here is my code:
shell_script_install_Project = 'Project_Install.sh'
print(f"shell_script_install_Project:{shell_script_install_Project}")
command_install_Project = f"""/usr/bin/osascript -e 'do shell script "/bin/bash {shell_script_install_Project} >> {os.environ['HOME']}/Project/log.log" with prompt "Project need to install some programs." with administrator privileges'"""
proc = subprocess.Popen(command_install_Project, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
(output, err) = proc.communicate()
print(output.decode("utf8"))
print(err.decode("utf8"))
returncode_command_install_Project = proc.wait()
print(f"returncode_command_install_Project : {returncode_command_install_Project}")
And here is the output:
shell_script_install_Project:Project_Install.sh
0:164: execution error: /bin/bash: Project_Install.sh: Operation not permitted (126)
0:164: execution error: /bin/bash: Project_Install.sh: Operation not permitted (126)
returncode_command_install_Project : 1
returncode_command_install_Project : 1
Process finished with exit code 0
I tried to run the script with sudo:
sudo python3 test2.py
But I get same issue:
shell_script_install_Project:Project_Install.sh
0:164: execution error: /bin/bash: Project_Install.sh: Operation not permitted (126)
returncode_command_install_Project : 1
How to execute a shell script from my python script in Mac OS X Catalina 10.15 without Operation not permitted (126) error message?
UPDATE:
I follow the instruction suggested in comment : "Change privilege of "bash" program in Security & Privacy. But I faced another problem:
I tried to do it by hand. I have a problem. I don't see the /bin/bash. And when I want to add it, I don't have access to the "bin" repertory.
So I did it for sh and I edit my code to execute my script with /bin/sh instead of /bin/bash
And I get same result:
shell_script_install_Projectt:Project_Install.sh
0:162: execution error: /bin/sh: Project_Install.sh: Operation not permitted (126)
I found how to add the bash in the "Full disk access" : https://apple.stackexchange.com/questions/376474/enabling-bin-bash-on-catalina-invisible-to-system-preferences-security-p
I restart my MAC and restart the terminal. But I still get the same issue!