How to format these two complex cron statements

cron

I need two very specific cron statements:

  1. A cron entry that would run on the 2nd Monday of each month every 4 hours beginning at 02:00 and execute the file /opt/bin/cleanup.sh

  2. A cron entry that would run at 18:15 on the 3rd day of every month ending in an "r" that executes /opt/bin/verrrrrrrry.sh

I've already tried various cron testers:

cron checker,cron tester,and cron translator

however none of them seem to be able to handle advanced cron expressions(or I do not know how to format the correct expression) as stated on

the cron trigger tutorial and
wikipedia

How can I check my cron statements? I obviously cannot wait for the actual event to pass so that the daemon may execute them.

Is there a good cron tester which supports advanced expressions? Or how to make the cron daemon parse the expression, or how to code these expressions?

What I have so far for these statements is:

  1. 0 2 * * 0#2 /opt/bin/cleanup.sh
  2. 15 18 3 * * /opt/bin/verrrrrrrry.sh

But of course these are not correct.

For #1, I do not know how to specify the '2nd Monday', nor 'every 4 hours', while still beginning at 02:00.

For #2, I have no idea how to only specify months ending in an 'r' except by manually coding them in. Nor do I know how to specify the 3rd day.

Best Answer

To have something execute only on the second Monday of a month the day of week value needs to be 1 and the day of month value has to be 8-14, the hour has to be 2,6,10,14,18,22 and the minute 0. However as dhag correctly commented and provided a solution for, when you specify both the day of week and the day of month (i.e. not as *), the program is executed when either matches. Therefore you have to test explicitly for either one, and the day of week is easier:

 0 2,6,10,14,18,22 8-14 * * test $(date +\%u) -eq 1 && /opt/bin/cleanup.sh

The final 1 determines the Monday and the range of day of month (8-14) picks it only when it is the second Monday.

The third day of every month ending in an "r" at 18:15:

 15 18 3 september,october,november,december * /opt/bin/verrrrrrrry.sh

(at least on Vixie cron you can use the names of the months. If yours does not support that you can replace that with 9-12)

Related Question