使用cron节点,我哪里出错了

Using cron-node, where did I go wrong?

本文关键字:出错 错了 cron 节点 使用      更新时间:2023-09-26

我需要定期更新一个文件,该文件会给我其他计划任务。

我唯一的问题是使用 cron-node

这里是测试代码(真实代码,缩小到 cron 问题,并带有一些测试线):

//Variable of the dayly update hour, with default value
var UPDATE_H=6,
    UPDATE_M=30;
function start(){
    console.log('start');
    //test_
    //Set the cron job at the next two minute following the start of the app
    time=new Date();
    time.setMinutes(time.getMinutes()+2);
    UPDATE_H=time.getHours();
    UPDATE_M=time.getMinutes();
    //_test
    //Start the cron job
    smil_update(UPDATE_H, UPDATE_M);
    //test_
    //Print a dot every minutes
    setInterval(function(){console.log('.');}, 60000);
    //_test
    player();
}
function smil_update(hour, min){
    var cronJob=require('cron').CronJob,
    when='',
    //test_
    time=new Date();
    //_test
    //Creating cron like date struct (cron like because seconds count too here)
    when='00 '+min+' '+hour+' * * *';
    console.log('Will work at:'+when);
    //test_
    console.log('It is '+time.getHours()+' '+time.getMinutes());
    //_test
    var job=new cronJob(when, timeZone='Europe/Paris', function(){
        console.log('UPDATE');
        player();
    });
    job.start();
}
function player(){
    console.log('Player');
}
start();

我得到:

start
Will work at:00 13 17 * * *
It is 17 11
Player
.
.
.

似乎我在某处错过了一点,但即使在重读文档后,我也不明白我做错了哪里。

我明白了。

错误出在 when 变量中。

而不是

when='00 '+...

它必须是

when='0 '+...

似乎 cron 不承认 00 为 0,出于某种未知原因。