[英]Running python script in Mac OSX launchd permission issue
I have a simple python script which is moving files from my download folder.我有一个简单的 python 脚本,它正在从我的下载文件夹中移动文件。 The script works fine when I running it via terminal.当我通过终端运行该脚本时,它运行良好。 The issue happens when it gets run through launchd:当它通过launchd运行时会发生此问题:
Traceback (most recent call last):
File "/Users/ben/Project/Automation/CleanDownload.py", line 11, in <module>
for f in listdir(downloadFolder):
OSError: [Errno 1] Operation not permitted: '/Users/ben/Downloads/'
Any Idea why?任何想法为什么?
Here is the workaround that I used to circumvent this issue.这是我用来规避此问题的解决方法。 I was trying to run a simple shell script, but the same workaround would apply to a Python script.我试图运行一个简单的 shell 脚本,但同样的解决方法也适用于 Python 脚本。 To summarize, the steps involved are:总结一下,涉及的步骤是:
More details:更多细节:
chmod +x
) and that you have the right #!
无论您想运行什么脚本,请确保它是可执行的(使用chmod +x
)并且您拥有正确的#!
line at the top of the script (eg #!/bin/bash
).脚本顶部的行(例如#!/bin/bash
)。 In this example, I'll use a script at ~/scripts/organize-screenshots.sh
that moves screenshots from my desktop to my Google Drive directory:在此示例中,我将使用~/scripts/organize-screenshots.sh
中的~/scripts/organize-screenshots.sh
将屏幕截图从我的桌面移动到我的 Google Drive 目录:#!/bin/bash
user_dir="/Users/soxley"
find "$user_dir"/Desktop -name 'Screen Shot *.png' -exec mv {} "$user_dir"/Google\ Drive/pictures/screenshots/ \;
/bin/bash -c "$1"
as the body of the script (see screenshot below)输入/bin/bash -c "$1"
作为脚本的主体(见下面的截图)run-script.app
in this example)单击“文件”>“保存”并将应用程序保存到您想要的任何位置(在本例中为run-script.app
)open -a run-script.app organize-screenshots.sh
执行命令open -a run-script.app organize-screenshots.sh
ProgramArguments
:使用以下ProgramArguments
更新您的 .plist :<key>ProgramArguments</key>
<array>
<string>open</string>
<string>-a</string>
<string>/Users/soxley/scripts/run-script.app</string>
<string>/Users/soxley/scripts/organize-screenshots.sh</string>
</array>
Now you should be able to run whatever script you want using this application as a wrapper.现在您应该能够使用此应用程序作为包装器运行您想要的任何脚本。
if you havent seen this check out "OSError: [Errno 1] Operation not permitted" when installing Scrapy in OSX 10.11 (El Capitan) (System Integrity Protection)如果您在 OSX 10.11 (El Capitan)(系统完整性保护)中安装 Scrapy 时还没有看到这个,请查看“OSError: [Errno 1] Operation not allowed”
and this https://apple.stackexchange.com/questions/339862/ls-operation-not-permitted-mojave-security还有这个https://apple.stackexchange.com/questions/339862/ls-operation-not-permitted-mojave-security
according to Roger go to Go to System Preferences -> Security & Privacy根据Roger 的说法,转到“系统偏好设置” ->“安全与隐私”
and give Full Disk Access to Terminal
.并为Terminal
提供 Full Disk Access。
https://discussions.apple.com/thread/8637915 https://discussions.apple.com/thread/8637915
您是否尝试过授予/sbin/launchd
全盘访问权限?
I was breaking my head on this issue for Big Sur for a LONG Time.很长一段时间以来,我一直在为 Big Sur 打破这个问题。 What worked for me was the following:对我有用的是以下内容:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.