2

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 ..
});
Santosh Shinde
  • 6,045
  • 7
  • 44
  • 68

3 Answers3

8

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
        }
      },
Colin Rauch
  • 515
  • 1
  • 6
  • 17
  • can you please give me the exact explanation for this ? – Santosh Shinde Dec 06 '16 at 07:31
  • Can you create a new file, put my code in there (change it to 1 hour maybe) and try again? That should work, since I just tested it and it worked fine. If that works for you as well, than you have a problem elsewhere in your code / environment. – Colin Rauch Dec 06 '16 at 09:28
  • '0 */2 * * *' please tell me this is for 2 hours right ? – Santosh Shinde Dec 06 '16 at 09:30
  • I found an error. It seems like the optional second is actually required. Which is strange since it says in their documentation that is not required. For every 2 hours you should try '0 0 */2 * * *' – Colin Rauch Dec 06 '16 at 09:34
  • and for the 12 hours ?? – Santosh Shinde Dec 06 '16 at 10:27
  • 1
    '0 0 */12 * * *' Try it with 1 hour or something like that first. You should really try it in a new file and just execute that file to see if the problem may be somehwere else. – Colin Rauch Dec 06 '16 at 10:30
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 ');
});

enter image description here

Adiii
  • 54,482
  • 7
  • 145
  • 148
1

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