简体   繁体   English

OS X 10.10.3 launchctl权限被拒绝

[英]OS X 10.10.3 launchctl Permission denied

I execute launchctl start com.xxx.xxx.plist 我执行launchctl start com.xxx.xxx.plist

I can find AutoMakeLog.err and the content : 我可以找到AutoMakeLog.err和内容:

Traceback (most recent call last):
File "/Users/xxxx/Downloads/Kevin/auto.py", line 67, in <module>
output = open(file_name, 'w')
IOError: [Errno 13] Permission denied: '2015-04-22-09:15:40.log'

plist content : plist内容:

<array>
  <string>/Users/xxxx/Downloads/Kevin/auto.sh</string>
</array>
<key>StartCalendarInterval</key>
<dict>
    <key>Minute</key>
    <integer>30</integer>
    <key>Hour</key>
    <integer>08</integer>
</dict>
<key>StandardOutPath</key>
<string>/Users/xxxx/Downloads/Kevin/AutoMakeLog.log</string>
<key>StandardErrorPath</key>
<string>/Users/xxx/Downloads/Kevin/AutoMakeLog.err</string>

auto.sh auto.sh

#!/bin/sh
/usr/bin/python /Users/xxxx/Downloads/Kevin/auto.py

auto.py auto.py

file_name = time.strftime('%Y-%m-%d-%H:%M:%S', time.localtime(time.time()))
file_name += '.log'
output = open(file_name, 'w')
output.writelines(response.text)
output.close()

auto.sh and auto.py have chomd 777 auto.sh和auto.py有chomd 777

PS: I direct execution auto.sh there is nothing error. PS:我直接执行auto.sh没有任何错误。

Even user-specific launchd jobs on OSX are executed with / as the current directory. 甚至OSX上的用户特定的 launchd作业也是/作为当前目录执行的。

Since auto.py is creating a file with a mere filename, without path , it effectively tries to create a file in / and fails due to insufficient privileges. 由于auto.py正在创建一个仅包含文件名的文件而没有路径 ,因此它会有效地尝试在/创建文件,并且由于权限不足而失败。

Thus, either change to a directory the current user can create files in, or specify an explicit path; 因此,要么更改当前用户可以在其中创建文件的目录,要么指定显式路径; eg (assuming import os ): 例如(假设import os ):

file_name = os.getenv('HOME') + '/' + time.strftime('%Y-%m-%d-%H:%M:%S', time.localtime(time.time()))

As for how you can run your python scripts directly , without the need for an intermediate shell script: 至于如何直接运行python脚本 ,而不需要中间shell脚本:

In your plist file, specify the command to execute as follows: 在plist文件中,指定要执行的命令,如下所示:

  <key>ProgramArguments</key>
  <array>
    <string>python</string>
    <string>/Users/xxxx/Downloads/Kevin/auto.py</string>
  </array>

Note that you needn't specify a path for python , because it is in the $PATH when launchd runs your job. 请注意,您无需为python指定路径,因为当launchd运行您的作业时,它位于$PATH

However, note that the $PATH contains fewer entries than what you see in Terminal , and a notable absence is /user/local/bin ; 但请注意, $PATH包含条目少于您在Terminal看到条目,并且值得注意的缺席/user/local/bin ; the value is (as of OSX 10.10.3): 值是(从OSX 10.10.3开始):

/usr/bin:/bin:/usr/sbin:/sbin

I'm adding this answer for visibility, although was not causing the original issue in this question. 我正在为可见性添加此答案,尽管在此问题中没有引起原始问题。

Due to changes in macOS 10.14 Mojave launchd commands have more security restrictions now. 由于macOS 10.14的变化, Mojave launchd命令现在有更多的安全限制。 If you see Permission denied errors for files and directories owned by your user, then see Q: How Do I Grant Root Access To User Files in Mojave for a possible fix. 如果您看到用户拥有的文件和目录的Permission denied错误,请参阅问:如何为Mojave中的用户文件授予root访问权限以获得可能的修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM