node-cron
node-cron copied to clipboard
How to run cron every minute
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();
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..