从哪里开始诊断简单的 cronjob

从哪里开始诊断简单的 cronjob

我有一个 Ubuntu 服务器,其中只有一个用户,并且有以下 crontab:

# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and crontab(8)
# 
# m h  dom mon dow   command
MAILTO=<my email>
* * * * * echo "Test"

虽然这个 cron 任务很简单,但我收不到任何电子邮件。我还尝试过按如下方式修改它(以防我的电子邮件出现问题):

* * * * * echo "Test" >> test.txt

但该文件test.txt从未被创建。

这看起来像真的问题很简单,但通常当我遇到 cron 作业问题时,它实际上正在运行,只是抛出了错误。当作业甚至没有运行时,我不知道如何开始诊断它。

更新输出service cron status

sbarnett@sbarnett:~$ service cron status
Failed to get properties: Access denied
sbarnett@sbarnett:~$ sudo service cron status
[sudo] password for sbarnett:
● cron.service - Regular background program processing daemon
   Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2017-08-15 14:15:52 MST; 3 weeks 1 days ago
     Docs: man:cron(8)
 Main PID: 1682 (cron)
   CGroup: /system.slice/cron.service
           └─1682 /usr/sbin/cron -f

Sep 07 09:55:01 sbarnett.vm CRON[3144]: pam_unix(cron:session): session opened for user root by (uid=0)
Sep 07 09:55:01 sbarnett.vm CRON[3145]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Sep 07 10:05:01 sbarnett.vm CRON[3151]: pam_unix(cron:session): session opened for user root by (uid=0)
Sep 07 10:05:01 sbarnett.vm CRON[3152]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Sep 07 10:05:01 sbarnett.vm CRON[3151]: pam_unix(cron:session): session closed for user root
Sep 07 10:15:01 sbarnett.vm CRON[3156]: pam_unix(cron:session): session opened for user root by (uid=0)
Sep 07 10:15:01 sbarnett.vm CRON[3157]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Sep 07 10:17:01 sbarnett.vm CRON[3159]: pam_unix(cron:session): session opened for user root by (uid=0)
Sep 07 10:17:01 sbarnett.vm CRON[3160]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Sep 07 10:17:01 sbarnett.vm CRON[3159]: pam_unix(cron:session): session closed for user root

答案1

我的问题似乎很奇怪,我并不完全了解其全部细节 - 但通过一些猜测和阅读各种论坛帖子,我找到了答案。

有时操作系统更新可能会影响 cron 文件的权限。以下任何一项(或全部)操作都可以修复此问题:

  • sudo service cron reload
  • sudo service cron restart
  • sudo /etc/init.d/cron restart
  • shutdown -r now
  • sudo apt-get dist-upgrade

基本上:

  • 尝试重新启动 cron 守护进程
  • 尝试重启电脑
  • 如果其他方法都失败了,可能会有一个更新来修复这个问题

答案2

在 Ubuntu 上,crontab 有点不同,时间选择器后有一个“用户”字段......

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly

要调试,您可以尝试使用一个简单的命令,例如:

logger "This goes to /var/log/syslog"

并检查是否在预期时间转到该日志文件。然后调用一个脚本(脚本本身会调用logger...)

相关内容