【发布时间】:2015-04-03 18:13:58
【问题描述】:
我在 Ubuntu 14.05 上设置了一个 cron 作业,以执行 Python 脚本。这个 Python 脚本应该获取一些数据,将其存储在数据库中并通过 Boxcar 向我的 iPhone 发送通知。问题是,我没有收到任何通知。另外,我检查了我的数据库。没有添加新数据,所以我很确定脚本没有执行。
我使用了以下命令:sudo crontab -e 并输入了以下内容:
30 09 * * * /usr/bin/python /home/jnevens/code/main.py
这应该在每天 9:30 执行main.py,对吧?
我的 Python 代码如下:
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()
我需要重新启动 cron 作业才能开始工作吗?还有什么我做错了吗?
【问题讨论】:
-
你应该可以在
/var/log/syslog看到脚本是否已经运行 -
更改cron时无需重启。
-
@mata 我的
syslog中有以下内容:Apr 3 09:30:01 dbDroplet CRON[3684]: (root) CMD (/usr/bin/python /home/jnevens/code/main.py) Apr 3 09:30:01 dbDroplet CRON[3683]: (CRON) info (No MTA installed, discarding output)。这是什么意思?