每小时的cron工作没有运行

时间:2012-09-17 05:12:56

标签: linux debugging cron

我用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每小时运行一次命令?

2 个答案:

答案 0 :(得分:5)

我的通灵调试功能告诉我create-shapshots.sh脚本 运行,但由于环境变量问题(可能是PATH)失败了。 Cron通常在清理环境中运行命令,所用内容远少于shell通常提供的内容。

答案 1 :(得分:0)

我同意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不会起作用。显然,手册页可能在撒谎。我不是百分百肯定,因为我没有那么多使用Anacron。当前的Cron设置将自动使用Anacron来运行预期运行的脚本,并因计算机关闭而以某种方式失败。