How can we run the node-cron job in every 12 hours interval?

风格不统一 提交于 2021-01-29 03:16:58

问题


I want to schedule the email after every 12 hrs , For that I have used node-cron.

I have used the following code but its not giving me actual result, so please help me to resolve this issue,

var job = new CronJob('0 0 */12 * * *', function(){
   //email send code ..
});

回答1:


Looking at the documentation the code should look like this:

var cron = require('node-cron');

cron.schedule('0 0 */12 * * *', function(){
  console.log('running a task every twelve hours');
});

Note: You need to have the app running all the time or the cron won't be executed.

And If you print the crone time on console the we wil get like as follows :

      cronTime: {
        source: '0 0 */12 * * *',
        zone: 'America/Los_Angeles',
        second: {
          '0': true
        },
        minute: {
          '0': true
        },
        hour: {
          '0': true,
          '12': true
        },
        dayOfMonth: {
          '1': true,
          '2': true,
          '3': true,
          '4': true,
          '5': true,
          '6': true,
          '7': true,
          '8': true,
          '9': true,
          '10': true,
          '11': true,
          '12': true,
          '13': true,
          '14': true,
          '15': true,
          '16': true,
          '17': true,
          '18': true,
          '19': true,
          '20': true,
          '21': true,
          '22': true,
          '23': true,
          '24': true,
          '25': true,
          '26': true,
          '27': true,
          '28': true,
          '29': true,
          '30': true,
          '31': true
        },
        month: {
          '0': true,
          '1': true,
          '2': true,
          '3': true,
          '4': true,
          '5': true,
          '6': true,
          '7': true,
          '8': true,
          '9': true,
          '10': true,
          '11': true
        },
        dayOfWeek: {
          '0': true,
          '1': true,
          '2': true,
          '3': true,
          '4': true,
          '5': true,
          '6': true
        }
      },



回答2:


You can try this module https://www.npmjs.com/package/node-schedule

var schedule = require('node-schedule');


var rule = new schedule.RecurrenceRule();


var j = schedule.scheduleJob('1 * * * * *', function(){
  console.log('Will run after every mint');
});



var rule = new schedule.RecurrenceRule();

rule.second=1;

var j = schedule.scheduleJob(rule, function(){
  console.log('this will run after every one seocnd ');
});




回答3:


Try this for 12 hours interval ...

var job = new CronJob('0 0 12 * * *', function(){

  //email send code ..
});

0 --> for seconds 0 --> for minutes

12--> for 12 hours interval



来源:https://stackoverflow.com/questions/40973124/how-can-we-run-the-node-cron-job-in-every-12-hours-interval

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!