Skip to content Skip to sidebar Skip to footer

Script Runs Fine In Terminal But Not From Launchd

My Python script runs fine in the terminal but when I try to set it up to run once a day at a certain time with launchd (set up using software called Lingon), I just can't get it t

Solution 1:

Based on the article here you need to create a .plist for launchd something like the following:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plistPUBLIC"-//Apple//DTD PLIST 1.0//EN""http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plistversion="1.0"><dict><key>Label</key><!-- The label should be the same as the filename without the extension --><string>org.yourusername.my_script-test</string><!-- Specify how to run your program here --><key>ProgramArguments</key><array><string>/usr/local/bin/python3</string><string>/Users/jeff/Documents/scripts/my_script.py</string></array><!-- Run every hour --><key>StartInterval</key><integer>3600</integer><!-- seconds --></dict></plist>

Then:

$ launchctl load ~/Library/org.yourusername.my_script-test.plist
$ launchctl start org.yourusername.my_script-test

An article here covers environment variables

Solution 2:

I've tried all that has been mentioned, special thanks to Padraic, but nothing seems to work. It's strange because the script runs perfect when run from the terminal but comes up with errors when run from launchd. I was able to get rid of the errors when run from launchd but then the script would not run from the terminal. Very strange. But here's how I did get it to run in both the terminal and on schedule from launchd. First, I changed the shebang line from this:

#!/usr/bin/env python3

to this:

#!/usr/bin/env /Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4

I then had to specify in the rest of the script the full path to files, for example, from this:

log = open('log_directory/my_log.log', 'a')

to this:

log = open('/Users/jeff/documents/my_script_documents/python/development/log_directory/my_log.log', 'a')

In any event, it all works now, but I believe the problem I've had may have something to do with having upgraded my Mac to the Yosemite OS. There's some mention regarding a possible bug in Yosemite concerning launchd/launchd.conf/launchctl. Well, I'd like to believe it was not me for the past 4 days trying to get this to work...but who knows?

Post a Comment for "Script Runs Fine In Terminal But Not From Launchd"