【问题标题】:Hourly cron job did not run每小时 cron 作业未运行
【发布时间】:2012-12-16 06:09:45
【问题描述】:

我用crontab -e 打开了我的 crontab 文件。然后我将它添加到我的 crontab 中:

SHELL=/bin/bash

@hourly /home/ec2-user/utilities/create-snapshots.sh

然后我保存了文件。大约一个小时后,cron 日志显示:

CROND[1876]: (ec2-user) CMD (/home/ec2-user/utilities/create-snapshots.sh)
CROND[1877]: (root) CMD (/usr/lib64/sa/sa1 -S DISK 1 1)
CROND[1892]: (root) CMD (run-parts /etc/cron.hourly)
run-parts(/etc/cron.hourly)[1892]: starting 0anacron
run-parts(/etc/cron.hourly)[1901]: finished 0anacron

但是我知道命令 /home/ec2-user/utilities/create-snapshots.sh 并没有实际运行,因为它会创建我的数据库的快照,但它没有。如果我直接在终端中运行命令/home/ec2-user/utilities/create-snapshots.sh,则可以运行它。

如何让 cron 每小时运行一次该命令?

【问题讨论】:

  • @GregHewgill 啊,所以它无权访问$PATH 变量?
  • 对不起,我把我的评论变成了答案。是的,这是一个开始寻找的好地方。

标签: linux debugging cron


【解决方案1】:

我的心理调试能力告诉我create-shapshots.sh 脚本已运行,但由于环境变量问题(可能是PATH)而失败。 Cron 通常在清理过的环境中运行命令,比在 shell 提示符下通常拥有的东西要少得多。

【讨论】:

  • 那么我是否必须手动初始化 crontab 中已在我的 .bash_profile 文件中设置的所有变量?有没有更简单的方法来做到这一点?
  • 如果您愿意,可以从您的 create-snapshots.sh 脚本中获取您的 .bash_profile。这可能是最简单的方法。
  • 所以从create-snapshots.sh内部运行source ~/.bash_profile?
【解决方案2】:

我同意 Greg 的观点,即您的环境变量可能与您的想法不同。如前所述,它从一个空的石板开始。要调试变量,您可以使用 set 命令:

set >~/some-file.txt

您可以包含您的 .bash_profile 文件,但如果您缺少 PATH、HOME 等,那将没有那么有用。首先,您需要设置这些基本变量(尽管应该为用户 cron 脚本设置 HOME。)要加载您的 bash 配置文件脚本,请执行此操作,假设 $HOME 已正确设置:

. ~/.bash_profile

但是,阅读 anacrontab 手册页我看到了这个:

@period_name delay job-identify command

The period_name can only be set to monthly at the present time.
This will ensure jobs are only run once a month, no matter the
number of days in this month, or the previous month.

所以感觉你的@hourly 不起作用。显然,手册页可能在撒谎。我不是 100% 确定,因为我没有那么多地使用 Anacron。当前的 Cron 设置将自动使用 Anacron 来运行预期会运行但由于计算机关闭而以某种方式失败的脚本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-18
    • 1970-01-01
    • 2022-01-03
    • 2017-05-26
    • 2011-09-19
    • 2019-11-19
    • 2021-10-23
    相关资源
    最近更新 更多