Reputation: 11982
I've set up a cron job on Ubuntu 14.05, to execute a Python script. This Python script should fetch some data, store it in a database and send a notification to my iPhone via Boxcar. Problem is, I did not receive any notification. Also, I have checked my database. No new data was added, so I'm pretty sure the script was not executed.
I've used the following command: sudo crontab -e
and entered the following:
30 09 * * * /usr/bin/python /home/jnevens/code/main.py
This should execute main.py
every day at 9:30, right?
My Python code is the following:
from tweet_mining import tweetFetcher
from rail import collectSchedules
from datetime import date, timedelta
from urllib import urlencode
from urllib2 import Request, urlopen
def go():
twitter = tweetFetcher()
tweetCount = twitter.main()
delta = timedelta(days=1)
d = date.today() - delta
collectSchedules(d, d)
push_not('Done!', 'Collected ' + str(tweetCount) + ' tweets')
return
def push_not(title, message):
url = 'https://new.boxcar.io/api/notifications'
values = {'user_credentials' : '####',
'notification[title]' : title,
'notification[sound]': 'echo',
'notification[long_message]': message}
data = urlencode(values)
req = Request(url, data)
response = urlopen(req)
return response.read()
go()
Do I need to reboot for the cron job to start working? Something else I'm doing wrong?
Upvotes: 0
Views: 116
Reputation: 401
I always adds path to interpreter in first script line
#!/usr/bin/python"
no in cron file. Maybe this is a problem.
You use Ubuntu, but in Centos this section in Your example: "/usr/bin/python" tell to cron deamon: what permissions the user has to run the script.
Upvotes: 0
Reputation: 2166
Make sure that you set an email address in your crontab if the server has mail routing enabled then you can receive a mail with the output.
Also make sure to redirect your errors to stdout.
30 09 * * * /usr/bin/python /home/jnevens/code/main.py 2>&1
Alternatively redirect your output to a local log file and inspect what is in it.
If you can, run the script every minute during testing so that you can see what is going wrong in real time. Usually the problem is some difference in environment and/or the PATH between your shell environment and the environment that cron runs under.
Upvotes: 1