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)?


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 2-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.

  • 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
  • 3
    @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
  • 3
    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
  • 1
    Fedora and RHEL 5,6,7 also do not like 0 as a day of month. As user31053 pointed out: 0 0 2-30/2 * * command works as expected. – NoelProf Jun 27 '14 at 15:55

I think a possibility is using the day of the year, such like this:

# for odd days
test $(((`date +%j` % 2))) != 0 && command

# for even days
test $(((`date +%j` % 2))) == 0 && command

It is tested for Unix and Linux systems.

  • I prefer this answer because it skips the problem with the odd number of days with some months. Anyway I suggest the dollar notation instead backticks: test $(($(date +%j) % 2)) == 0 && command – caligari Apr 24 '18 at 10:31
  • I have reviewed that %j is not the Julian date, so the best code avoiding the New Year transition must be calculated with seconds: test $(($(date +%s) / 86400 % 2)) == 0 && command – caligari Apr 24 '18 at 10:54
  • Thanks for the comments! Our proposal worked for us like a charm. We used that scheme to run every day crons on different server nodes, all they sharing the same crontab, but the script only allowing to run the script on one of them. However, if we need to make it more specific, we will consider your proposal. Thanks!!! – Jordi Apr 25 '18 at 15:46

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.)

  • 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 Apr 23 '14 at 11:38

In general, I would run it every day and have the script use logic to determine it if it should be run today.

Making a simple status file telling when last run and then compairing would work very easily.

If it needs to be run through different sources, make it argument-dependant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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