我想使用 anacron 安排每日任务。阅读一些资料后,我将以下行添加到 /etc/anacrontab:
@daily 10 price_scrape /home/pawel/Project_Inflation/run.sh >> /home/pawel/Project_Inflation/log/task_log 2>> /home/pawel/Project_Inflation/log/error_log
它可以工作(脚本按预期执行),但它每小时执行一次,而它应该每天执行一次。以下是输出grep 'cron' /var/log/syslog
:
Jun 27 15:02:24 pawel-ubuntu anacron[399]: Anacron 2.3 started on 2019-06-27
Jun 27 15:02:24 pawel-ubuntu anacron[399]: anacron: /etc/anacrontab: Unknown named period on line 14, skipping
Jun 27 15:02:24 pawel-ubuntu anacron[399]: /etc/anacrontab: Unknown named period on line 14, skipping
Jun 27 15:02:24 pawel-ubuntu anacron[399]: Will run job `price_scrape' in 10 min.
Jun 27 15:02:24 pawel-ubuntu anacron[399]: Jobs will be executed sequentially
Jun 27 15:12:24 pawel-ubuntu anacron[399]: Job `price_scrape' started
Jun 27 15:12:53 pawel-ubuntu anacron[399]: Job `price_scrape' terminated
Jun 27 15:12:53 pawel-ubuntu anacron[399]: Normal exit (1 job run)
Jun 27 15:17:01 pawel-ubuntu CRON[721]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Jun 27 16:02:24 pawel-ubuntu systemd[1]: Started Run anacron jobs.
Jun 27 16:02:24 pawel-ubuntu anacron[2762]: Anacron 2.3 started on 2019-06-27
Jun 27 16:02:24 pawel-ubuntu anacron[2762]: /etc/anacrontab: Unknown named period on line 14, skipping
Jun 27 16:02:24 pawel-ubuntu anacron[2762]: anacron: /etc/anacrontab: Unknown named period on line 14, skipping
Jun 27 16:02:24 pawel-ubuntu anacron[2762]: Will run job `price_scrape' in 10 min.
Jun 27 16:02:24 pawel-ubuntu anacron[2762]: Jobs will be executed sequentially
Jun 27 16:12:24 pawel-ubuntu anacron[2762]: Job `price_scrape' started
Jun 27 16:12:53 pawel-ubuntu anacron[2762]: Job `price_scrape' terminated
在我看来,anacron 实际上是由 cron 本身在系统启动时运行的,但令我惊讶的是,有一个 anacron 服务和相关的 anacron 计时器每小时触发一次。但是,当我停止服务和计时器时,该作业根本不会运行。有什么建议可以解释为什么不遵守每日周期吗?
谢谢
答案1
anacrontab
状态手册页
目前 period_name 仅可设置为每月。
所以你的daily
将无法工作并导致错误信息anacron: /etc/anacrontab: Unknown named period on line 14, skipping
答案2
对我来说,更好的方法是放入一个可执行脚本,/etc/cron.daily/
这样你就不用再担心了。