Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cron expression for every five minutes in the next n hours?

I know every five minutes is:

0 0/5 * * * *

But how do I limit the number hours for this to happen?

Example: Every five minutes for the next 10 hours.

like image 780
Alex Pi Avatar asked Sep 25 '13 20:09

Alex Pi


People also ask

How do I schedule a cron job to run every 5 minutes?

Make a new line at the bottom of this file and insert the following code. Of course, replace our example script with the command or script you wish to execute, but keep the */5 * * * * part as that is what tells cron to execute our job every 5 minutes.

What is the cron expression for every 5 minutes?

Run a Cron Job Every 5 Minutes*/5 means create a list of all minutes and run the job for every fifth value from the list.

What is the use of * * * * * In cron?

It is a wildcard for every part of the cron schedule expression. So * * * * * means every minute of every hour of every day of every month and every day of the week .

How do I schedule a cron job every 10 minutes?

Run a Cron Job after every 10 minutes The slash operator helps in writing the easy syntax for running a Cron job after every 10 minutes. In this command, */10 will create a list of minutes after every 10 minutes.


2 Answers

Use the range operator to specify the hours. For example, for running the job every five minutes for 10 hours starting at 2 am:

0 0/5 2-12 * * *

Here is an excellent tutorial on Cron expression and operators: http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/crontrigger.html

Edit: 10/3/2016: Updated the link.

like image 177
Khurram Avatar answered Oct 25 '22 10:10

Khurram


I think should be able to define a trigger that can repeat every hour until a certain time:

import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

...

Trigger myTrigger = newTrigger()
                    .withIdentity('myUniqueTriggerID")
                    .forJob(myJob)
                    .startAt(startDate)
                    .endAt(endDate)
                    .withSchedule(simpleSchedule().withIntervalInHours(1));

...

scheduler.scheduleJob(myJob, myTrigger);
like image 33
Morfic Avatar answered Oct 25 '22 10:10

Morfic