Cron Expression to be executed every n weeks

1.6k Views Asked by At

I am trying to write a Cron expression that triggers every n weeks.

I have thought about something like:

0   0  */21 *   *

2013-09-01 00:00:00
2013-09-22 00:00:00
2013-10-01 00:00:00
2013-10-22 00:00:00

Per this Cron tester

But it triggers every 1st in addition to the 21st.

Ideas?

2

There are 2 best solutions below

0
On

If you're using Quartz, then you may be able to accomplish that schedule with a SimpleTrigger instead:

     Trigger trigger = newTrigger() 
         .withIdentity(triggerKey("myTrigger", "myGroup"))
         .withSchedule(simpleSchedule()
             .repeatHourlyForever(n * 7 * 24))
         .startAt(...)
         .build();
0
On

The '/' syntax specifies the increment during the period and not a repeat interval. Admittedly a subtle and confusing difference.

In this case there is only one available increment (21 days) during the 1 month period. The first number specifies the value to start with, in this case 0. Specifying '*' before the '/' is equivalent to specifying 0. So the job will only fire on the first day and at 21 days.

If you only want to fire the job once a month and not repeatedly then you could use the expression 0 0 21 * *.

If you want a job to trigger at a regular interval then you can use a Quartz SimpleTrigger with a repeatInterval specified.