I’m running a simple python script sending data to a mongodb
#!/usr/bin/env python
import sys
import time
from datetime import datetime
import pymongo
from pymongo import Connection
today = { 'date and time' : datetime.today() }
connection = Connection()
db = connection.tests
collection = db.times
collection.insert(today)
And I’m trying to use cron to schedule this every minute. I’ve used crontab to set this
* * * * * /Users/MyUser/XX/YY/ZZ/timetest.py
And I can execute this perfectly using python timetest.py from the correct directory; however the program is still not running on its own. I feel like I’m very close to getting it to work, can anyone help me with this?
It is likely that the cron environment does not match your user’s environment. In cron you can either set the path variable in crontab like
or you can just explicitly call the python binary on your script
or you can set the shebang line in your script to reference the python binary explicitly (this may not be desirable if you ever use virtualenv)