node-cron icon indicating copy to clipboard operation
node-cron copied to clipboard

How to run cron every minute

Open soubhikchatterjee opened this issue 3 years ago • 1 comments

I am using the following code to run a cronjob every minute, I copied the corn expression from https://crontab.cronhub.io/ but it does not seem to console.log() every 1 minute.

var CronJob = require("cron").CronJob;

var date1 = new Date();

var job = new CronJob(
  "*/1 * * * *",
  function () {
    var date2 = new Date();
    const diffTime = Math.abs(date2 - date1);
    console.log(Math.round(diffTime / 1000));
  },
  null,
  true,
  "Asia/Kolkata"
);
job.start();

soubhikchatterjee avatar Jul 23 '21 11:07 soubhikchatterjee

Try this expression: "0 */1 * * * *". This adds an extra astrisk and the initial 0 states which part (second) of the minute to run: 1:00.. 2:00, 3:00..

alfanhui avatar Aug 01 '21 15:08 alfanhui