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

gro*_*kus 6 crontab cron-jobs

每小时我都会收到一封包含这样错误的电子邮件,

Subject: Cron <root@supa> root    cd / && run-parts --report /etc/cron.hourly

/bin/sh: root: not found
Run Code Online (Sandbox Code Playgroud)

/etc/crontab 的内容如下,无论是否删除用户“root”(第 6 列),我都会收到相同的错误。

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h 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 )
#
Run Code Online (Sandbox Code Playgroud)

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

$ ll /etc/cron.hourly/
total 0
lrwxrwxrwx 1 root root 25 2009-10-29 09:24 ntpsync -> /home/<user>/bin/ntpsync
lrwxrwxrwx 1 root root 28 2009-10-23 10:33 foo -> /home/<user>/bin/foo
Run Code Online (Sandbox Code Playgroud)

第一个脚本如下,

$ 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
Run Code Online (Sandbox Code Playgroud)

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

$ sudo /etc/init.d/cron restart
Run Code Online (Sandbox Code Playgroud)

我的Ubuntu版本如下,

$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=8.04
DISTRIB_CODENAME=hardy
DISTRIB_DESCRIPTION="Ubuntu 8.04.1"
Run Code Online (Sandbox Code Playgroud)

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

$ sudo crontab -l
# m h  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 )
Run Code Online (Sandbox Code Playgroud)

Las*_*sen 6

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

# m h 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 )
Run Code Online (Sandbox Code Playgroud)

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

apt-get update
apt-get install cron
Run Code Online (Sandbox Code Playgroud)

更新:

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

minute hour dayOfMonth month dayOfWeek userToRunAs restOfLineIsCommand
Run Code Online (Sandbox Code Playgroud)

另一种类型是用户 crontab 的这可以通过使用crontab. 实际配置位于/var/spool/cron/crontabs/USERNAME并始终以拥有它的用户身份执行,因此该文件的格式为:

minute hour dayOfMonth month dayOfWeek restOfLineIsCommand
Run Code Online (Sandbox Code Playgroud)