4

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.

2
  • "Specify months ending in an 'r'"... You mean months whose names have "r" as their last letter O_o? So, in English, September to December? That's a peculiar specification.
    – dhag
    Commented Apr 2, 2015 at 21:42
  • Yes, that's exactly what is meant.
    – rivanov
    Commented Apr 2, 2015 at 21:44

3 Answers 3

5

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)

2
  • When both day and day-of-week are specified, they are OR-ed together: your first job runs every Monday and on days 8-14. I suggest testing for Mondays within the command, perhaps 0 2 8-14 * * test $(date +\%u) -eq 1 && /opt/bin/cleanup.sh. (This misses the other requirement "every four hours starting at 2", which you got right.)
    – dhag
    Commented Apr 2, 2015 at 21:33
  • @dhag you are right, it says so in the man pages.
    – Anthon
    Commented Apr 2, 2015 at 21:40
2

The format is minute / hour / day of month / month / day of week

So that would make

0 4 9-15 * 2 /opt/bin/cleanup.sh and

15 18 3 8-14 * /opt/bin/verrrrrrrry.sh

4
  • If the 1st is a Monday, then the second Monday is the 8th of the month.
    – Anthon
    Commented Apr 2, 2015 at 21:31
  • You're right, corrected.
    – Jan
    Commented Apr 2, 2015 at 21:32
  • And that only runs it at 4 am, not a 2 am, 6am, etc
    – Anthon
    Commented Apr 2, 2015 at 21:33
  • As the question doesn't specify until when it's to be run every 4h, I omitted this, as it can be easily adapted.
    – Jan
    Commented Apr 2, 2015 at 21:34
1

A cron entry that would run on the 2nd Monday of each month every 4 hours beginning at 02:00

00 02/4 8-14 * * [ `date +\%u` = 1 ] && /opt/bin/cleanup.sh
  • http://crontab.guru/#00_02/4_8-14_*_1

A cron entry that would run at 18:15 on the 3rd day of every month ending in an "r"

15 18 3 9-12 * /opt/bin/verrrrrrrry.sh
  • http://crontab.guru/#15_18_3_9-12_*
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .