Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

When configuring cron to run a command every other day using the "Day of Month" field, like so:

1 22 */2 * * COMMAND

it runs every time the day of month is odd: 1,3,5,7,9 and so on.

How can I configure cron to run on days of month that are even like 2,6,8,10 and so on (without specifying it literally, which is problematic as every month has a different number of days in the month)?

share|improve this question
add comment

2 Answers

up vote 25 down vote accepted

The syntax you tried is actually ambigous. Depending on how many days are in the month, some months it will run on odd days and some on even. This is because the way it is calculated takes the total number of posibilities and divides them up. You can over-ride this strage-ish behavior by manually specifying the day range and using either an odd or even number of days. Since even day scripts would never run on the 31st day of longer months, you don't loose anything using 30 days as the base for even-days, and by specifying specifically to divide it up as if there were 31 days you can force odd-day execution.

The syntax would look like this:

# Will only run on odd days:
0 0 1-31/2 * * command

# Will only run on even days:
0 0 0-30/2 * * command

Your concern about months not having the same number of days is not important here because no months have MORE days than this and for poor February, the date range just won't ever match the last day or two, but it will do no harm having it listed.

The only 'gotcha' for this approach is that if you are on an odd day cycle, following months with 31 days your command will also run on the first of the month. Likewise if you are forcing an even cycle, each leap year will cause one three day cycle and the end of February. You cannot really get around the fact that any regular pattern of "every other day" is not always going to fall on even or odd days in every month and any way you force this you will either have an extra run or be missing a run between months with miss-matched day counts.

share|improve this answer
1  
Thanks, but what will happen on months like February where you have only 28 days? The star actually takes care of that - but is indeed ambiguous. –  freddie Jul 5 '11 at 12:58
2  
@freddie: See my edited answer ... but it's a non-issue because out of range values will just be ignored, nothing will happen on the 30th or 31st of February. Ever. You could manually specify with a list like 0,2,4...,30,32,34 and it wouldn't matter, the out of range values would just never get matched. –  Caleb Jul 5 '11 at 13:01
1  
Thanks! I understand, thank for you the informative answer! –  freddie Jul 5 '11 at 13:03
1  
On Ubuntu server 8.04, it seems that the syntax using day-of-month zero is invalid (bad day-of-month). The following syntax, however, is accepted: 0 0 2-30/2 * * command –  user31053 Jan 24 '13 at 7:37
add comment

Let's check every day if it's an "other" :-) (bc program required)

0 0 * * * test $(echo `date +%s` / 86400 % 2 == 0 |bc) -eq 0 && command

(I'm not sure the code appears correctly. The date +%s part is between back-apostrophes.)

share|improve this answer
    
This will run every other day but does not answer the question! It will still run sometimes on odd days sometimes on even days depending on the month. This has the same result as the code in the question, you are just arriving at the result by doing your own math on the seconds since epoch. This runs on even days since epoch but not on even days of our calendar. –  Caleb 20 hours ago
add comment

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.