简体   繁体   English

如何使用节点计划在指定的时间运行任务?

[英]how to run task on specified time using node-schedule?

I want to run node task every day 8AM using node-schedule package https://www.npmjs.com/package/node-schedule but instead of everyday 8AM its running every minute. 我想每天使用节点计划包https://www.npmjs.com/package/node-schedule运行上午8点的节点任务,而不是每天上午8点运行。 what is the correct method that i can apply to rule to achieve my task ? 我可以运用规则来完成任务的正确方法是什么? we are using node-schedule on linux not sure if format is different in that case. 我们在Linux上使用node-schedule ,不确定在这种情况下格式是否不同。

cron.js cron.js

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

module.exports = function CronJob() {
    var rule = new cronSchedule.RecurrenceRule();
    rule.hour = 8;
    var dailyJob = cronSchedule.scheduleJob(rule, function() {
    console.log('Testing 8AM');
            async.eachSeries(directories, function (dir, cb1) {
                var dir = __dirname + dir;
                // get files for the directory
                });
            }, function (err, fileInfos) {
                if (err) {
                    console.info('error', err);
                    return;
                }
            });
    });
}

If you want to execute task automatically and it will be recurring then you can try below code: 如果您想自动执行任务并将重复执行,则可以尝试以下代码:

var schedule = require('node-schedule');
var scheduleFunction = schedule.scheduleJob('0 8 * * *', function(){
  console.log('Yeah ! i did.'));
});

The above code will be executed daily at 08:00 AM. 上面的代码将每天08:00 AM执行。

Or if you want to execute code once at the start of each quarter then try below code: 或者,如果您想在每个季度初执行一次代码,请尝试以下代码:

var myRule = {hour: 0, minute: 0, dayOfWeek: 1, month: [0, 3, 6, 9]};
var scheduleFunction = schedule.scheduleJob(myRule, function(){
  console.log('will execute every quarter'));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM