4

Possible Duplicate:
cron : how to schedule to run first Sunday of every month

I want to execute a php script on every first sunday in month at 7 am . I entered the following line in the servers crontab.

0 7 1-7 * 0      user /path/to/script.php

Today I saw that the script did run on wednesday morning. How could that happen? I thought that the last 0 defines sunday?

Thanks for your help in advanced.

Community
  • 1
  • 1
Sebastian Schmidt
  • 1,078
  • 7
  • 17

3 Answers3

9

From the man page:

Commands are executed by cron(8) when the minute, hour, and month of year fields match the current time, and when at least one of the two day fields (day of month, or day of week) matches the current time (see ``Note'' below).

and later:

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, are not *), the command will be run when either field matches the current 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.

In other words, your script runs on the 1st through the 7th as well as any Sunday.

GaryO
  • 5,873
  • 1
  • 36
  • 61
  • That's really unfortunate. nnCron [apparently](http://www.nncron.ru/help/EN/working/cron-format.htm) does this the expected way (with AND): "`0 9 1-7 * 1 * First Monday of each month, at 9 a.m.`" But that not really helpful for any general unix system. – jwadsack Jan 04 '17 at 23:36
2

Fields 3 and 5 don't work together like you're assuming.

You'll need to set field 5 to * and modify your php script to check whether the current day is a Sunday before it continues.

AJ.
  • 27,586
  • 18
  • 84
  • 94
2

Your cron specification comes out to this:

0 - run at minute 0
7 - run at hour 7
1-7 - run on days 1,2,3,4,5,6,7
* - all months
0 - day 0 of the week

I don't believe you can restrict it to only the first type-of-day via the tools available in cron. You could try

0 7 * * 0

which'd run every sunday, but there's no method to say "only the first Sunday", so this would run every Sunday. You can, however, modify your script to check which sunday it's being run on, and simply exit if it's not the first one.

Marc B
  • 356,200
  • 43
  • 426
  • 500