0

Im trying to run a python script using launchd on OS X. When I run it from the terminal using the following commands, it works:

$ /Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 /Users/me/path/to/script.py

Here is my launchd plist file that has those same commands under the ProgramArguments key, but it doesn't work.

<?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>net.me.my-script</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4</string>
        <string>/Users/me/path/to/script.py</string>
    </array>
    <key>StartInterval</key>
    <integer>86400</integer>
</dict>
</plist>

The launchd initially didnt work, so I tried running it in the terminal manually, and I got an error and realized that I needed to refer to files I was reading and writing to in the python script with their full path, so I fixed that. That made the script run in terminal but it didn't fix the problem in launchd (yes, I've unloaded and loaded it multiple times).

I ran launchctl list and found a 1 exit code listed next to my plist, then I checked /var/log/system.log and found the error message Service exited with abnormal code: 1 but that's all the error messages I can find.

Based on 'last modified' dates and stuff from finder, it seems like it opens an xml file in the python script, but it doesn't write anything to it. It also doesn't seem to open a json file in the script.

Again, all this stuff works just fine run manually in the terminal, so I'm thinking it has to be a plist or launchd issue.

4
  • Where are the files your script is trying to read and write located, and what permissions do they have? Commented May 23, 2016 at 5:28
  • they're just an xml and a json file and they're in the same location as the script. they have full r/w permissions for everyone. the full file path of both of the files is specified in the script, which i discovered i needed to do after the first error occured Commented May 23, 2016 at 5:39
  • Is it a launchdaemon or a launchagent (location also) and what owner:group is set on the .plist? Regarding theService exited with abnormal code: 1 error; what does the python script actually do? You should see other events in Console.app right around the same time that might be related. Commented May 23, 2016 at 5:59
  • its an agent, located in /Users/me/Library/LaunchAgents. i assume I'm the owner but I'll check when I get back to my Mac later. the script gets xml data and parses it then copies certain fields to a google spreadsheet and other fields to a json file that acts as a log. I looked around system.log for anything near the abnormal code message and nothing stood out. I'll check again and I'll also check Console.app later Commented May 23, 2016 at 13:35

1 Answer 1

1

Solved. I was able to set the StandardErrorPath and StandardOutPath keys and see the errors the script was throwing. I was getting a few UnicodeEncodeErrors on instances of f.write() and f.read() when manipulating the xml file, but only when running the script through launchd. I replaced with open('file.xml', 'w') as f: with with io.open('file.xml', 'w', encoding='utf8') as f: and the same for the read instances, and it works! Anyone know why this worked fine in my IDE and in terminal but not in launchd?? All using same interpreter.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.