node-schedule(定时任务)

https://www.npmjs.com/package/node-schedule

示例:每五分钟执行脚本

var schedule = require('node-schedule');
let cp = require('child_process');
const path = require('path');

/*=================================================
*  cron job for repairing shop pending count
* =================================================
*/
schedule.scheduleJob('*/5 * * * *', function() {
    console.log('cronJob : shopPendingCount, execution time :' + getDate());
    cp.exec('node shopPendingCount.js', { cwd: path.join(process.cwd(), 'tools/scripts') }, function(err, stdout, stderr){
        if(err) {
            console.log(err);
            return;
        }
        console.log(stdout);
    });
});


function getDate() {
    var myDate = new Date();
    var year = myDate.getFullYear();
    var month = myDate.getMonth() + 1;
    var date = myDate.getDate();
    var h = myDate.getHours();
    var m = myDate.getMinutes();
    var s = myDate.getSeconds();
    return  year + '-' + convert(month) + "-" + convert(date) + " " + convert(h) + ':' + convert(m) + ":" + convert(s);
}

function convert(s) {
    return s < 10 ? '0' + s : s;
}

Last updated

Was this helpful?