Reputation: 1015
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 to run. From everything I've read, it's better to invoke the Python script from a shell script (I'm on a Macbook, running Yosemite). So, that's what I'm trying to do. The errors I get when the script is due to be run are:
TERM environment variable not set.
env: python3: No such file or directory
At this point, I'm pretty sure it's an environment issue but no matter what I try, I just can't get it to run. By the way, I can get the shell script to run on the schedule this way:
#!/bin/bash
echo "hello world."
The problem comes when I try to run this:
#!/bin/bash
/Users/jeff/Documents/scripts/my_script.py
Also, even though I've been working with computers for a long time, I'm still pretty ignorant about a lot of things, so please tell me how to fix this like I'm a newbie.
Upvotes: 0
Views: 1705
Reputation: 1015
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?
Upvotes: 1
Reputation: 180441
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 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>
<!-- 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
Upvotes: 1