【问题标题】:NodeJS 'Cron' Package - schedule job which runs after every 20 secondsNodeJS 'Cron' 包 - 计划每 20 秒运行一次的作业
【发布时间】:2018-06-18 18:04:03
【问题描述】:

我必须每 20 秒运行一次 cron 作业。所以在做了一些搜索之后,我发现我们需要使用像这样的格式'0/20 * * * * *'。但是这种格式不能正常工作。该作业不是在每 20 秒后运行一次,而是在 1 分钟后运行。你能告诉我我需要在这里设置什么才能每 20 秒运行一次作业吗?

const queue = new CronJob({
  cronTime: '0/20 * * * * *', 
  onTick: function() {
    console.log('<=============Perform Job==========>',new Date());
    performActivity();
  },
  start: false,
  timeZone: 'Asia/Calcutta'
});

queue.start();

【问题讨论】:

    标签: javascript node.js cron


    【解决方案1】:

    每 20 秒应该是*/20:

    cronTime: '*/20 * * * * *', 
    

    【讨论】:

      【解决方案2】:

      试试

      "cronTime":"*/20 * * * * *"

      "cronTime":"00,20,40 * * * * *"

      【讨论】:

        【解决方案3】:

        问题出在“0/20”,要每20秒执行一次,你应该使用“*/20”。

        这里是基于您的代码的示例:

        const CronJob = require('cron').CronJob;
        const queue = new CronJob({
            cronTime: '*/20 * * * * *',
              onTick: function() {
                  console.log('<=============Perform Job==========>',new Date());
              },
              start: false,
              timeZone: 'Europe/Madrid'
        });
        
        queue.start();
        

        【讨论】:

          猜你喜欢
          • 2018-03-22
          • 2023-03-04
          • 2011-03-20
          • 2018-02-18
          • 2017-10-22
          • 2021-06-17
          • 2010-09-12
          • 1970-01-01
          相关资源
          最近更新 更多