Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crontab / Running cronjob on first sunday in month [duplicate]

Tags:

php

crontab

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.

like image 912
Sebastian Schmidt Avatar asked Dec 12 '22 12:12

Sebastian Schmidt


2 Answers

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.

like image 121
GaryO Avatar answered Dec 17 '22 23:12

GaryO


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.

like image 41
AJ. Avatar answered Dec 18 '22 00:12

AJ.