Node cron, run every midnight

前端 未结 5 1715
粉色の甜心
粉色の甜心 2020-12-17 11:03

I want to run cron job daily at midnight. For this I am using

0 0 0 1-31 * * 

but it doesn\'t work for me. I am using the node cron. Plea

相关标签:
5条回答
  • 2020-12-17 11:10

    Here is:

    var CronJob = require('cron').CronJob;
    
    var job = new CronJob('00 00 00 * * *', function() {
      /*
       * Runs every day
       * at 00:00:00 AM. 
       */
       // DO SOMETHING
      }, function () {
        /* This function is executed when the job stops */
      },
      true /* Start the job right now */
    );
    
    0 讨论(0)
  • 2020-12-17 11:12

    It's quite simple....

    The below is the code to run crone job every day 12 AM..

    var job = new CronJob('0 0 0 * * *', function() {
     //will run every day at 12:00 AM
    })
    

    For more https://www.npmjs.com/package/cron

    0 讨论(0)
  • 2020-12-17 11:15

    You don't need to set all the fields. Set just first three and it'll take care of running every day at midnight
    0 0 0 * * *

    0 讨论(0)
  • 2020-12-17 11:19

    You can try this format too.

    var CronJob=require('cron').CronJob;
    var cronJob1 = new CronJob({
    
        cronTime: '00 00 00 * * * ',
        onTick: function () {
        //Your code that is to be executed on every midnight
        },
        start: true,
        runOnInit: false
    });
    

    To understanding something more about cronTime, See the following codes:

    cronTime: '00 */3 * * * * ' => Executes in every 3 seconds.

    cronTime: '* */1 * * * * ' => MEANING LESS. Executes every one second.

    cronTime: '00 */1 * * * * ' => Executes every 1 minute.

    cronTime: '00 30 11 * * 0-5 ' => Runs every weekday (Monday to Friday) @ 11.30 AM

    cronTime: '00 56 17 * * * ' => Will execute on every 5:56 PM

    0 讨论(0)
  • 2020-12-17 11:34
    0 0 * * *
    

    This pattern will run CronJob job daily at 00:00

    https://crontab.guru/examples.html

    0 讨论(0)
提交回复
热议问题