AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 3115
Accepted
grokus
grokus
Asked: 2010-08-26 11:28:44 +0800 CST2010-08-26 11:28:44 +0800 CST 2010-08-26 11:28:44 +0800 CST

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

  • 772

每小时我都会收到一封带有此类错误的电子邮件,

Subject: Cron <root@supa> 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

# 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 )
#

我的 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

第一个脚本如下所示,

$ 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
# 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 )
cron-jobs crontab
  • 5 5 个回答
  • 14037 Views

5 个回答

  • Voted
  1. Best Answer
    LassePoulsen
    2010-08-26T12:57:35+08:002010-08-26T12:57:35+08:00

    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 )
    

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

    apt-get update
    apt-get install cron
    

    更新:

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

    minute hour dayOfMonth month dayOfWeek userToRunAs restOfLineIsCommand
    

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

    minute hour dayOfMonth month dayOfWeek restOfLineIsCommand
    
    • 6
  2. Michael Terry
    2010-08-26T12:25:30+08:002010-08-26T12:25:30+08:00

    我知道您说删除第六列中的“root”后仍然会出现错误,但看起来确实是问题所在。

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

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

    所以我会说尝试再次删除它。

    • 1
  3. raphink
    2010-08-26T12:58:43+08:002010-08-26T12:58:43+08:00

    你的/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
    
    # 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 )
    #
    

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

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

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

    • 1
  4. Marco Ceppi
    2010-08-26T13:56:26+08:002010-08-26T13:56:26+08:00

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


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


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

    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    * * *   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 )
    #
    

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

    • 1
  5. user11267
    2011-02-23T01:20:04+08:002011-02-23T01:20:04+08:00

    这样做:

    # crontab -r
    

    不要这样做:

    # crontab /etc/crontab
    

    相反,手动编辑文件/etc/crontab 。

    • 0

相关问题

  • 管理员应该如何阅读 root 的邮件?

  • 备份 bash 脚本未压缩其 tarball

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何安装 .run 文件?

    • 7 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    如何获得 CPU 温度?

    • 21 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Marko Smith

    如何使用命令行将用户添加为新的 sudoer?

    • 7 个回答
  • Marko Smith

    更改文件夹权限和所有权

    • 9 个回答
  • Marko Smith

    你如何重新启动Apache?

    • 13 个回答
  • Marko Smith

    如何卸载软件?

    • 11 个回答
  • Marko Smith

    如何删除 PPA?

    • 26 个回答
  • Martin Hope
    NES 如何启用或禁用服务? 2010-12-30 13:03:32 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    Olivier Lalonde 如何在结束 ssh 会话后保持进程运行? 2010-10-22 04:09:13 +0800 CST
  • Martin Hope
    David B 如何使用命令行将用户添加为新的 sudoer? 2010-10-16 04:02:45 +0800 CST
  • Martin Hope
    Hans 如何删除旧内核版本以清理启动菜单? 2010-08-21 19:37:01 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve