I can't figure out how to set up CRON job for range time in one line - cron

I can't figure out how to set up CRON job for range time in one line.
Example 1) : Every 5 min from 00:30 to 01:00 on the workdays
Expecting Cron job : 30,35,40,45,50,55 0 * * 1-5 but missing at 1:00
If config 0,30,35,40,45,50,55 0,1 * * 1-5
==> the issue is it will run 00:00,1:00,1:30,1:35,1:35,1:40,1:45,1:50,1:55 which are out of our scope.
Example 2) : Every 10 min from 01:00 to 04:30 on the workdays
Expecting Cron job : 0,10,20,30,40,50, 1-4 * * 1-5
The out of scope is a running at 4:40,:4:50
Could someone please help this query ?

I don't think you can write single line configs for these ranges because cron runs at every minute specified for every hour as you've illustrated. Make it as simple as possible with two lines:
30,35,40,45,50,55 0 * * 1-5
0 1 * * 1-5
0,10,20,30,40,50, 1-3 * * 1-5
0,10,20,30 4 * * 1-5

Related

node-cron run job every 3 hours

I am trying to run a node-cron job every 3 hours and I am not sure if I am doing it right.
Right now I am using:
* * */8 * * *
Is this correct?
You should zero-out the second and minute values, and use a step of /3.
The cron expression for this is
0 0 */3 * * *
Which evaluates to 'At 0 seconds, 0 minutes every 3rd hour'.
Your current expression * * */8 * * * would try to run every second of every minute past every 8th hour.

How to write a cron for every 30 minutes between 2AM to 4AM?

I was trying to figure a cron for every 30 minutes between 2AM to 4AM?
So the cron run time will be: 2:00 2:30 3:00 3:30 4:00
Every hour will be something like this:
0 2,3,4 * * * command
Thank You.
I'd just write it as two distinct rules:
0,30 2-3 * * * /run/this/command
0 4 * * * /run/this/command
If you're the sort that worries about this sort of thing (I'm not), you can use a conditional to get it onto one line:
0,30 2-4 * * * [[ "$(date +%H%M)" != "0430" ]] && /run/this/command
This will run the command given at 4:30 as well, but not actually call your script unless the time is something other than 4:30.

Executing cron in a time range

I have been searching but nothing resolved my question.
I have a cron job every 2 minutes (*/2 * * * * /example.sh) and I want the cron does not run in a time range, between 23:30 - 00:30
I have tried (30 00-23 * * * /example.sh) but It didn't work.
Any ideas? Thanks.

How to write multiple cron expression

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

Are these cron expressions equivalent?

Is there any diffenrence between
*/5 * * * * <COMMAND>
and
0/5 * * * * <COMMAND>
?
It is not the same.
The first one will be executed every 5 minutes starting any moment, while
the second one will be executed on minutes 0, 5, 10,... 55.
Only if the first one is executed on minute 5k for the first time, they will have the same behaviour.
Every 5 minutes can be written like this:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * <COMMAND>
this
0/5 * * * * <COMMAND>
or
*/5 * * * * <COMMAND>
Anyway, last one will start any time the minute changes and then keep a distance of 5 minutes to next execution.
The * character means every. If it is alone, it will mean every minute, every hour, etc.
The / character can be used to specify increments to values. If we indicate X/Y it means every Y minutes starting at minute X.
0/15 = every 15th minute of the hour, starting at minute zero = 0,15,30,45
3/20 = every 20th minute of the hour, starting at minute three = 3,23,43
/40 = every 40th minute of the hour= 40
3/40 = every 40th minute of the hour, starting at minute three= 43
Coming back to your question, the use of both operators gets the following result:
*/5 = every 5 minutes starting anytime. This way, it will start whenever changes the minute and repeat after 5 minutes, 10, etc.
0/5 = every 5 minutes starting on minute 0. It will be internally considered as: 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55 as these are the minutes that suit the condition +5 minutes coming from start at minute 0.
Some references:
CronTrigger
Linux "crontab every" examples

Resources