Crontab ranges

Latest response

I have created a crontab that is supposed to only run on certain days of the month, but it is not working. Instead, it runs every Sunday.

Here is the crontab:

m h dom mon dow command

0 0 15-21 * 0 nonprod.sh
0 5 15-21 * 0 nonprod-reboot.sh
0 0 22-28 * 0 prod.sh
0 0 22-28 * 0 prod.sh

Is the syntax I am using for ranges not valid?

Responses

The '0' should be '*' in your last field before the command, 0 denotes Sunday in the dow field.

Oh, that is what I want. The nonprod scripts should run the 3rd Sunday of the month, and prod should run on the 4th Sunday.

It's not an issue with ranges, it's apparently expected cron behaviour

Note: The day of a command's execution can be specified by two fields --
       day of month, and day of week.  If  both  fields  are  restricted  (ie,
       aren't  *),  the command will be run when either field matches the cur-
       rent time.  For example,
       "30 4 1,15 * 5" would cause a command to be run at 4:30 am on  the  1st
       and 15th of each month, plus every Friday.*

http://unixhelp.ed.ac.uk/CGI/man-cgi?crontab+5

The way I would solve this is to run the script in the date ranges and have a check in the script (or in cron) to see if it is Sunday, and only execute if the day matches. Alternatively, you could call the script every Sunday and check it is within the 'day of month' ranges you are looking for.

Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.