我想用cron在Linux(ubuntu)上每小时运行一个python程序。 我写了一个名为script.sh的脚本
cd Dropbox/NetworkProject/AMT_Crawler/ scrapy crawl AmtCrawler --set FEED_URI=data.json --set FEED_FORMAT=json
然后我用
crontab -e
并添加一个这样的线
*/30 * * * * sh Dropbox/NetworkProject/AMT_Crawler/script.sh 2>&1 >> /Dropbox/NetworkProject/AMT_Crawler/output.log
之后,我跑了
sudo /etc/init.d/cron start
在terminal。 它说
不要通过/etc/init.d来调用init脚本,而是使用service(8)实用程序,例如service cron start。因为你试图调用的脚本已经被转换成了Upstart作业,所以你也可以使用start(8)实用程序,例如启动cron
所以我运行这个
service cron start
然后有一个错误:
start: Rejected send message, 1 matched rules; type="method_call", sender=":1.196" (uid=1000 pid=12574 comm="start cron ") interface="com.ubuntu.Upstart0_6.Job" member="Start" error name="(unset)" requested_reply="0" destination="com.ubuntu.Upstart" (uid=0 pid=1 comm="/sbin/init")
现在我坚持这个问题寻求一些帮助。
cron(8)
总是在运行。 使用crontab -e
更改crontab(5)
文件时,无需启动或重新启动它:
Additionally, cron checks each minute to see if its spool directory's modtime (or the modtime on /etc/crontab) has changed, and if it has, cron will then examine the modtime on all crontabs and reload those which have changed. Thus cron need not be restarted whenever a crontab file is modified. Note that the crontab(1) command updates the modtime of the spool directory whenever it changes a crontab.
(最后一句是为什么总是build议在修改自己的crontab(5)
文件时使用crontab(1)
程序。)
我不知道这是否是整个问题,但是你的输出redirect是颠倒过来的。 而不是:
command 2>&1 >> /path/to/output.log
(它将stderrredirect到stdout,然后redirectstdout, 而不是stderr到output.log
),您需要:
command >> /path/to/output.log 2>&1
正如其他人所说, service cron start
命令不应该是必要的; crond
应该已经在运行了(正如你已经通过获得一个空的日志文件来确认的那样)。
尝试sudo service cron start
。
*/30 * * * * sh Dropbox/NetworkProject/AMT_Crawler/script.sh 2>&1 >> /Dropbox/NetworkProject/AMT_Crawler/output.log
您需要完整的path为您的收件箱目录。 它不在/下。
试试这个(假设Dropbox目录是$HOME/Dropbox
):
*/30 * * * * sh $HOME/Dropbox/NetworkProject/AMT_Crawler/script.sh >> $HOME/Dropbox/NetworkProject/AMT_Crawler/output.log 2>&1
请注意,这会更改对Dropbox
目录的引用。 它还修复了redirect操作符的sorting; 看到我的(基思汤普森)的答案。