Shubham
Shubham

Reputation: 1191

NodeJS 'Cron' Package - schedule job which runs after every 20 seconds

I've to run cron job at every 20 seconds. So after did some search, I found that we need use format like this '0/20 * * * * *'. But this format not working properly. Instead of running the job after every 20 seconds, The job runs after 1 minute. Can you please let me know what I need to set here to run the job after every 20 seconds?

const queue = new CronJob({
  cronTime: '0/20 * * * * *', 
  onTick: function() {
    console.log('<=============Perform Job==========>',new Date());
    performActivity();
  },
  start: false,
  timeZone: 'Asia/Calcutta'
});

queue.start();

Upvotes: 1

Views: 2259

Answers (3)

wolmi
wolmi

Reputation: 1709

The problem is on the "0/20", to execute every 20 seconds you should use "*/20".

Here an example based on your code:

const CronJob = require('cron').CronJob;
const queue = new CronJob({
    cronTime: '*/20 * * * * *',
      onTick: function() {
          console.log('<=============Perform Job==========>',new Date());
      },
      start: false,
      timeZone: 'Europe/Madrid'
});

queue.start();

Upvotes: 1

Danny Galiyara
Danny Galiyara

Reputation: 217

Try

"cronTime":"*/20 * * * * *"

or

"cronTime":"00,20,40 * * * * *"

Upvotes: 2

Metalik
Metalik

Reputation: 943

Every 20 seconds should be */20:

cronTime: '*/20 * * * * *', 

Upvotes: 2

Related Questions