![](/img/trans.png)
[英]Crontab can't execute python script with error: “[Errno 1] Operation not permitted”
[英]How to execute a shell script from my python script in Mac OS X Catalina 10.15 without Operation not permitted (126) error message?
環境:
Mac OS Catalina 10.15.7
蟒蛇 3.10
問題:
我正在嘗試從 Mac OS X Catalina 10.15 中的 python 腳本執行 shell 腳本。
這是我的代碼:
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}")
這是輸出:
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
我嘗試使用 sudo 運行腳本:
sudo python3 test2.py
但我遇到同樣的問題:
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
如何在 Mac OS X Catalina 10.15 中從我的 python 腳本執行 shell 腳本而沒有 Operation not allowed (126) 錯誤消息?
更新:
我按照評論中建議的說明進行操作:“在安全和隱私中更改“bash”程序的權限。但我遇到了另一個問題:
我試着用手做。 我有個問題。 我沒有看到 /bin/bash。 當我想添加它時,我無權訪問“bin”目錄。
所以我為 sh 做了它,我編輯我的代碼以使用 /bin/sh 而不是 /bin/bash 執行我的腳本
我得到相同的結果:
shell_script_install_Projectt:Project_Install.sh
0:162: execution error: /bin/sh: Project_Install.sh: Operation not permitted (126)
我發現如何在“全盤訪問”中添加 bash: https ://apple.stackexchange.com/questions/376474/enabling-bin-bash-on-catalina-invisible-to-system-preferences-security-p
我重新啟動我的 MAC 並重新啟動終端。 但我仍然遇到同樣的問題!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.