chancyWu
chancyWu

Reputation: 14403

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

I want to write a python script to call 'brew update' daily in my Mac. The below is the python script and launch 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>

When i run using the launchd load the plist file, there is the same error as below:

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

If run the python script directly in shell, it works well. But when call in the .plist, there is No such file error.

Upvotes: 0

Views: 1101

Answers (1)

Enrico Granata
Enrico Granata

Reputation: 3329

I suspect that this is because the launchd environment is not setting up a PATH that matches what you would have in your own shell

You might need to pass the absolute path to the brew binary, or setup an Environment section in your plist that contains the PATH you want to use

Upvotes: 1

Related Questions