Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have such entry in my crontab:

0 0 1-2 * 1-6

It is correct that it would run at 00:00, 1st or 2nd day of the month from Monday to saturday? For example, if 1st day will be sunday then it would not run it, right? But instead it would run a cronjob at 2nd day of the month because it will be not a sunday.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

If the first day of the month is a Sunday it will indeed run.

The reason is that the two conditions "day of month" (third field) and "day of week" (fifth field) are not connected with an AND (both must occur) but with an OR (either one must occur). From man 5 crontab:

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.

So your cronjob will run at midnight every 1st and 2nd of the month and additionally every Mon, Tue, Wed, Thu, Fri and Sat of the month.

If you need to run the command only once in a month but not on Sunday, as you write in your comment, you must check the date in the command itself.

These are the conditions to run the command:

IF (date is 1st day of month AND date is not Sunday) 
    OR (date is 2nd day of month AND yesterday was Sunday) 
THEN run command

You could write two cronjobs:

0 0 1 * * test $(date +%u) -eq 7 || echo "today's the 1st day of the month but not Sun"
0 0 2 * * test $(date +%u) -eq 1 && echo "today's Mon (=yesterday was Sun) and the 2nd day of the month"

The first cronjob will run every 1st day of the month at midnight:

  • date +%u returns an integer where 1 is Mon, 2 is Tue, etc.

  • test checks whether $(date +%u1) equals 7 (Sunday).

  • || is the OR boolean operator, it reacts to the return value of the previous command. If test returns FALSE then the rest of the command is run.

The second cronjob will run every 2nd day of the month at midnight:

  • Again, date +%u returns an integer where 1 is Mon, 2 is Tue, etc.

  • test checks whether $(date +%u1) equals 1 (Monday).

  • && is the AND boolean operator, if test returns TRUE then the rest of the command is run.

share|improve this answer
    
Thanks what does mean test in "test $(date +%u) -eq 7"? –  Heihachi Feb 1 at 8:30
    
@Heihachi I edited my answer to make that clear. See man test for more information. –  jaume Feb 1 at 8:49
    
Got it. But if 1st and 2nd day of the month are monday and tuesday, cron would run 2 times in one month. I need to run command only once in a month but not on sunday. Better place for that is in bash script itself? –  Heihachi Feb 1 at 8:55
    
Thanks for clarifying that. I edited my answer, I propose that you write two cronjobs (you could also write one, but the test statement would be complex.) –  jaume Feb 1 at 9:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.