Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

schedule a job for every 5 minutes

I'm using the following job scheduler code to print Today is recognized by Rebecca Black!- every day at 12AM.

// executes every day at 12:AM
var rule = new schedule.RecurrenceRule();
rule.dayOfWeek = [0, new schedule.Range(1, 6)];
rule.hour = 15;
rule.minute = 14;
schedule.scheduleJob(rule, function() {
console.log(rule);
    console.log('Today is recognized by Rebecca Black!---------------------------');
});

How can I print for every 5 minutes I used the following way but it is not working...

var rule = new schedule.RecurrenceRule();

rule.minute = 5;
schedule.scheduleJob(rule, function() {
console.log(rule);
    console.log('Today is recognized by Rebecca Black!---------------------------');
});
like image 616
Psl Avatar asked Jan 03 '14 06:01

Psl


People also ask

How do I schedule a cron job 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?

“At every 5th minute.” Cron job every 5 minutes is a commonly used cron schedule. We created Cronitor because cron itself can't alert you if your jobs fail or never start. Cronitor is easy to integrate and provides you with instant alerts when things go wrong.

How do I schedule a cron job every minute?

How does it work? The asterisk (*) operator specifies all possible values for a field. For example, an asterisk in the hour time field would be equivalent to every hour or an asterisk in the month field would be equivalent to every month. An asterisk in the every field means run given command/script every minute.

How do we schedule repeated jobs?

If you need to repeat a task on a schedule, you can use the cron service. The cron service is a time based job scheduling system that starts when the system boots. Every minute the cron service checks whether any scheduled jobs to run, and if so, it runs them.


2 Answers

var rule = new schedule.RecurrenceRule();

rule.minute = new schedule.Range(0, 59, 5);

schedule.scheduleJob(rule, function(){
    console.log(rule);
    console.log('Today is recognized by Rebecca Black!---------------------------');
});
like image 80
vinayr Avatar answered Sep 18 '22 17:09

vinayr


You can use the cron format:

var event = schedule.scheduleJob("*/5 * * * *", function() {
        console.log('This runs every 5 minutes');
    });

The cron format consists of:

*    *    *    *    *    *
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    |
│    │    │    │    │    └ day of week (0 - 7) (0 or 7 is Sun)
│    │    │    │    └───── month (1 - 12)
│    │    │    └────────── day of month (1 - 31)
│    │    └─────────────── hour (0 - 23)
│    └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)
like image 40
J.C. Gras Avatar answered Sep 17 '22 17:09

J.C. Gras