OSError: [Errno 2] No such file or directory when call in launchd plist file

OSError: [Errno 2] No such file or directory when call in launchd plist file

我想编写一个 python 脚本来每天在我的 Mac 中调用 'brew update'。 下面是 python 脚本和启动 plist。

#!/usr/local/bin/python
import subprocess

# update homebrew and check outdated package
subprocess.call(["brew", "update"])
subprocess.call(["brew", "outdated"])
subprocess.call(["brew", "doctor"])

.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>chancy.daily</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/python</string>
        <string>/Users/wuchangxi/Workspace/scripts/jobs/daily.py</string>
    </array>
    <key>StandardErrorPath</key>
    <string>/usr/local/var/log/chancy.daily.log</string>
    <key>StandardOutputPath</key>
    <string>/usr/local/var/log/chancy.daily.log</string>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Minute</key>
        <integer>42</integer>
        <key>Hour</key>
        <integer>12</integer>
    </dict>
</dict>
</plist>

当我运行使用launchd加载plist文件时,出现如下相同的错误:

Traceback (most recent call last):
  File "/Users/wuchangxi/Workspace/scripts/jobs/daily.py", line 5, in <module>
    subprocess.call(["brew", "update"])
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 522, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

如果运行 python 脚本直接在shell 中,效果很好。但是在.plist中调用时,没有这样的文件错误。

我怀疑这是因为 launchd 环境没有设置与您自己的路径匹配的路径 shell

您可能需要将绝对路径传递给 brew 二进制文件,或者在您的 plist 中设置一个包含您要使用的 PATH 的环境部分