How to write multiple cron expression
Asked Answered
T

5

6

Execute the job on Monday until Saturday from 7pm until 9am and the whole day for Sunday.

I try to input multiple expressions of cron, but it's not working. Can anyone get me the solution for this?

1. " * * 19-8 ? * MON,TUE,WED,THU,FRI,SAT "
2. " * * * ? * SUN "
Tubby answered 19/4, 2013 at 18:2 Comment(5)
whats java have to do with this?Pasteurism
What does this have to do with java?Bushore
cron (unix) or Quartz?Jacobus
Those expressions are related to time curfew that suppose user cannot log in into system during non working hour. But those expression is not valid. Why? Is it my cron expression is wrong?Tubby
@PaulVargas, it is quartzTubby
J
0

CronMaker is a utility which helps you to build cron expressions. CronMaker uses Quartz open source scheduler. Generated expressions are based on Quartz cron format.

This expressions defines the start of a task. It does not define its duration (it belongs to the task).

- used to specify ranges. For example, "10-12" in the hour field means "the hours 10, 11 and 12"

CronTrigger Tutorial

Jacobus answered 19/4, 2013 at 18:26 Comment(0)
N
5

Since you are using Quartz, you can create several different CronTriggers, and schedule all of them to your required job. E.g.(change the cron expressions to the expressions that you need)

SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();

JobDetail job = newJob(SimpleJob.class)
    .withIdentity("job1", "group1")
    .build();

Set<Trigger> triggers = new HashSet<>();

CronTrigger trigger = newTrigger()
    .withIdentity("trigger1", "group1")
    .withSchedule(cronSchedule("0/20 * * * * ?"))
    .build();
triggers.add(trigger1);

CronTrigger trigger2 = newTrigger()
    .withIdentity("trigger2", "group1")
    .withSchedule(cronSchedule("15 0/2 * * * ?"))
    .build();
triggers.add(trigger2);

CronTrigger trigger3 = newTrigger()
    .withIdentity("trigger3", "group1")
    .withSchedule(cronSchedule("0 0/2 8-17 * * ?"))
    .build();
triggers.add(trigger3);

scheduler.scheduleJob(job, triggers, false);

You can't create one trigger with multiple CronExpressions.

Nunatak answered 7/10, 2014 at 13:16 Comment(0)
C
1

If one needs for nodejs, https://github.com/datasert/cronjs does multi cron expressions. For ex., * 21-23 * * 2,4,6|* 0-5 * * 1,3,5|* * ? * 7

Disclaimer I'm part of the team which builds that library

Courson answered 29/8, 2021 at 3:52 Comment(0)
P
0

I think seeing complexity of your requirement we need to create 4 cron expression for your task to complete.

// task for Monday 7 PM to 12 PM

==>

 * 19-24 * * 1  <YOUR_TASK>
    ->* – every Minute 
    ->19-24 hours
    ->* – Every day
    ->* – Every month
    ->1--Mon,

//TASK for Tuesday to Friday ==>

* 00-24 * * 2-5  <YOUR_TASK>
->* – 0th Minute 
-> 00-24 hours
->* – Every day
->* – Every month
->1-5 -Mon, Tue, Wed, Thu , Fri, Sat

//task for Saturday upto 9 AM ==>

* 00-09 * * 6  <YOUR_TASK>
->00 – every Minute 
->00-09 – upto 9 AM
->* – Every day
->* – Every month
->6 -, Sat

//task for Saturday ==>

 * * * * 7   <YOUR_TASK>
    ->* – Every minute  
    ->00-09 – upto 9 AM
    ->* – Every day
    ->* – Every month
    ->6 -, Sat
Phosgene answered 19/4, 2013 at 18:17 Comment(0)
J
0

CronMaker is a utility which helps you to build cron expressions. CronMaker uses Quartz open source scheduler. Generated expressions are based on Quartz cron format.

This expressions defines the start of a task. It does not define its duration (it belongs to the task).

- used to specify ranges. For example, "10-12" in the hour field means "the hours 10, 11 and 12"

CronTrigger Tutorial

Jacobus answered 19/4, 2013 at 18:26 Comment(0)
C
0

cron-utils introduced a multi-cron notation: you can combine multiple crons into a single expression. Below an example:

String multicron = "0 0|0|30|0 9|10|11|12 * * ? *";
parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ));
Cron cron = parser.parse(multicron);
assertEquals(multicron, cron.asString());

The cron notation is the same as for any regular cron: the fields that hold the same values across the crons, remain the same. The fields that would hold different values, will have them separated by pipes.

Your two crons could be expressed as:

"* * 19-8|* ? * MON,TUE,WED,THU,FRI,SAT|SUN"

Currently cron-utils does not support jobs execution, but provides means to know next/previous execution date.

Chihli answered 24/3, 2018 at 4:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.