简体   繁体   English

每小时Cron作业未运行

[英]Hourly cron job did not run

I opened my crontab file with crontab -e . 我使用crontab -e打开了crontab文件。 Then I added this to my crontab: 然后将其添加到我的crontab中:

SHELL=/bin/bash

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

Then I saved the file. 然后,我保存了文件。 After about an hour, the cron logs displayed: 大约一个小时后,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

However I know that the command /home/ec2-user/utilities/create-snapshots.sh was not actually run because it would have created snapshots of my database, which it did not. 但是我知道命令/home/ec2-user/utilities/create-snapshots.sh实际上并没有运行,因为它会创建数据库的快照,但实际上并没有。 Running the command /home/ec2-user/utilities/create-snapshots.sh works if I run it directly in the terminal. 如果直接在终端中运行/home/ec2-user/utilities/create-snapshots.sh命令,则该命令/home/ec2-user/utilities/create-snapshots.sh

How can I get cron to run that command hourly? 我怎样才能让cron每小时运行一次该命令?

My psychic debugging powers tell me that the create-shapshots.sh script was run, but it failed because of an environment variable problem (possibly PATH ). create-shapshots.sh调试能力告诉我, create-shapshots.sh脚本运行,但是由于环境变量问题(可能是PATH )而失败。 Cron normally runs commands with a cleaned-out environment, with much less stuff than what you normally have at a shell prompt. Cron通常在清理后的环境中运行命令,所包含的内容比通常在shell提示符下要少得多。

I agree with Greg that your environment variables may not be what you think. 我同意Greg的观点,即您的环境变量可能与您的想法不符。 As mentioned it starts with an empty slate. 如前所述,它以一块空板开始。 To debug your variables you can use the set command: 要调试变量,可以使用set命令:

set >~/some-file.txt

You may include your .bash_profile file, although if you're missing PATH, HOME, etc. that won't be that useful. 您可能会包含.bash_profile文件,尽管如果您缺少PATH,HOME等,那将不会有用。 First you'd need to setup those basic variables (although HOME should be set for a user cron script.) To load you bash profile script do this, assuming $HOME is properly set: 首先,您需要设置这些基本变量(尽管应该为用户cron脚本设置HOME。)要加载bash配置文件脚本,请执行以下操作,假设正确设置了$ HOME:

. ~/.bash_profile

However, reading the anacrontab manual page I see this: 但是,阅读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.

So it feels like your @hourly won't work. 因此,您的@hourly感觉无法正常工作。 Obviously the manual page may be lying. 显然,手册页可能在说谎。 I'm not 100% sure as I'm not using Anacron that much. 我不确定百分百,因为我没有那么多使用Anacron。 The current Cron setup will automatically make use of Anacron to run scripts that were expected to run and somehow failed because the computer was off. 当前的Cron设置将自动使用Anacron来运行预期会运行的脚本,并且由于计算机关闭而以某种方式失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM