2012-09-17 283 views
1

我用crontab -e打开了我的crontab文件。然后我将其添加到我的crontab中:每小时cron作业没有运行

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按小时运行该命令?

+0

@GregHewgill啊,所以它不能访问'$ PATH'变量? – user730569

+0

对不起,我的评论取而代之。是的,这是一个开始寻找的好地方。 –

回答

6

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

+0

那么,我必须手动初始化已经在我的'.bash_profile'文件中设置的crontab中的所有变量吗?有没有更简单的方法来做到这一点? – user730569

+0

如果你愿意,你可以从你的'create-snapshots.sh'脚本获取'.bash_profile'。这可能是最简单的方法。 –

+0

所以从'create-snapshots.sh'内运行'source〜/ .bash_profile'? – user730569

0

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

set >~/some-file.txt 

您可以在您的.bash_profile文件,但如果你错过了路径,主等,这将不会是有用的。首先你需要设置的基本变量(虽然家庭应该对用户的cron脚本中设置。)加载你来砸配置文件脚本做到这一点,假设$ 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来运行预计会运行的脚本,并且因为计算机关闭而以某种方式失败。