我的cron.hourly配置有什么问题?

每隔一小时我收到一封错误的电子邮件,

Subject: Cron  root cd / && run-parts --report /etc/cron.hourly /bin/sh: root: not found 

/ etc / crontab的内容如下,要么我删除用户“root”或不删除(第6列),我得到相同的错误。

 SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # mh dom mon dow user command 11 * * * * root cd / && run-parts --report /etc/cron.hourly 25 6 * * * test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ) 47 6 * * 7 test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly ) 52 6 1 * * test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly ) # 

我的cron.hourly目录中有两个文件,

 $ ll /etc/cron.hourly/ total 0 lrwxrwxrwx 1 root root 25 2009-10-29 09:24 ntpsync -> /home//bin/ntpsync lrwxrwxrwx 1 root root 28 2009-10-23 10:33 foo -> /home//bin/foo 

第一个脚本如下,

 $ cat ~/bin/ntpsync #!/usr/bin/env bash echo "user: $USER" if [[ "$USER" == "root" ]] ; then ntpdate ntp.ubuntu.com else sudo ntpdate ntp.ubuntu.com fi 

即使我删除了/etc/cron.hourly/目录中的两个脚本,我仍然每小时收到相同的错误电子邮件。 我试图重新启动cron,我仍然收到相同的错误电子邮件。 我的下一个想法是重启,但我会避免这种情况。

 $ sudo /etc/init.d/cron restart 

我的Ubuntu版本如下,

 $ cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=8.04 DISTRIB_CODENAME=hardy DISTRIB_DESCRIPTION="Ubuntu 8.04.1" 

更新:我之前从/ etc / crontab文件中删除了第6列“root”,因为当我在网上搜索时,有人提到可以解决问题。 现在我认为问题在于我正在搞乱系统crontab配置而不是root的配置。

 $ sudo crontab -l # mh dom mon dow command 17 * * * * root cd / && run-parts --report /etc/cron.hourly 25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ) 47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly ) 52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly ) 

cron包中的默认crontab文件(3.0pl1-100ubuntu2.1这是ubuntu 8.04的最新版本)如下所示:

 SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # mh dom mon dow user command 17 * * * * root cd / && run-parts --report /etc/cron.hourly 25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ) 47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly ) 52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly ) 

您应该只能将其粘贴到文件中,但您可能还需要确保拥有最新版本的软件包。 你可以这样做:

 apt-get update apt-get install cron 

更新:

有两种不同类型的crontab,一种是系统的crontab,位于/etc/crontab 。 这个crontab有这个:

 minute hour dayOfMonth month dayOfWeek userToRunAs restOfLineIsCommand 

另一种类型是用户crontab,可以使用crontab对其进行修改。 实际配置位于/var/spool/cron/crontabs/USERNAME ,并始终以拥有它的用户身份执行,并且该文件的格式为:

 minute hour dayOfMonth month dayOfWeek restOfLineIsCommand 

我知道你说你在删除第六列中的“root”后仍然会收到错误,但它确实看起来像是问题。

例如,查看其他行。 他们都以“测试”开始。 那不是用户,这是命令的开始。 删除“root”会使命令以“cd”开头。

特别是因为错误消息说它找不到“root”,这是你在尝试运行不存在的程序时得到的错误。

所以我要说再试一次。

您的/etc/crontab确实很有趣。 实际上每一行实际上都应该有一个用户列,这是最有趣的部分。 例如,我读到:

 # /etc/crontab: system-wide crontab # Unlike any other crontab you don't have to run the `crontab' # command to install the new version when you edit this file # and files in /etc/cron.d. These files also have username fields, # that none of the other crontabs do. SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # mh dom mon dow user command 17 * * * * root cd / && run-parts --report /etc/cron.hourly 25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ) 47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly ) 52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly ) # 

顺便说一下,触摸这个文件通常不是一个好主意。 如果您需要添加更多通用的crontabs,请使用/etc/cron.d 。 您可以尝试使用以下命令恢复cron包的默认configuratino:

 $ sudo apt-get install --reinstall --yes -o DPkg::Options::=--force-confmiss -o DPkg::Options::=--force-confnew cron 

并看看它是否解决了这个问题。

这里真的有两个问题。 一个(更明显的)是root的个人crontab中不正确的第6列。 第二个无声的 – 是在/etc/crontab的每小时cron行未正确执行之后的命令。 修复如下:


您可以通过运行sudo crontab -r删除伪用户crontab文件


完成后,您需要在每小时cron行之后的/etc/crontab文件中为每行添加root用户 – 如下所示:

 SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # mh dom mon dow user command 11 * * * * root cd / && run-parts --report /etc/cron.hourly 25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ) 47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly ) 52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly ) # 

这应解决这些电子邮件问题。

这样做:

 # crontab -r 

不要这样做

 # crontab /etc/crontab 

而是手动编辑文件/ etc / crontab