简体   繁体   中英

Hourly cron job did not run

I opened my crontab file with crontab -e . Then I added this to my 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:

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. Running the command /home/ec2-user/utilities/create-snapshots.sh works if I run it directly in the terminal.

How can I get cron to run that command hourly?

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 ). Cron normally runs commands with a cleaned-out environment, with much less stuff than what you normally have at a shell prompt.

I agree with Greg that your environment variables may not be what you think. As mentioned it starts with an empty slate. To debug your variables you can use the set command:

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. 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:

. ~/.bash_profile

However, reading the anacrontab manual page I see this:

@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. Obviously the manual page may be lying. I'm not 100% sure as I'm not using Anacron that much. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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