fs.unlink方法没有在linux服务器上执行?

时间:2017-04-24 14:47:01

标签: javascript node.js linux fs cron-task

我在compareDates上执行CronJob方法,删除所有超过30天的文件,它在本地windows上运行良好,但我没有看到此方法在linux上执行。我知道如何纠正这个问题?

ctrl.js

var directories = ['/../../logs/dit', '/../../logs/st','/../../logs/uat'];
var currentDate = new Date();
module.exports = function CronJob() {
    var rule = new cronSchedule.RecurrenceRule();
    rule.hour = 8;
    rule.minute = 0;
    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
                fs.readdir(dir, function (err, files) {
                    if (err) return cb1(err);

                    // loop through each file
                    async.eachSeries(files, function (file, cb2) {
                        var filePath = path.resolve(dir + '/' + file);
                        // get info for the file
                        fs.stat(filePath, function (err, stats) {
                            if (err) return cb2(err);
                            var fileInfo = { fileDate: stats.birthtime, filename: file };
                            compareDates(fileInfo, filePath);
                            cb2(null, fileInfo);
                        });
                    }, cb1);

                });
            }, function (err, fileInfos) {
                if (err) {
                    console.info('error', err);
                    return;
                }
            });
    });
}

    function compareDates(file,path) {
            console.log('Comapre Method',file);
            var timeDiff = Math.abs(currentDate.getTime() - file.fileDate.getTime());
            var dayDifference = Math.ceil(timeDiff / (1000 * 3600 * 24));
            if (dayDifference >= 30) {
                  fs.unlink(path, function(err) {
                            if (err) {
                                // file doens't exist
                                console.info("File doesn't exist, won't remove it.");
                            } else {
                                console.log('removed file', file);
                            }
                        });

            }

    }

console.log数据

Comapre Method { fileDate: Wed Apr 19 2017 00:36:26 GMT-0400 (EDT),
  filename: 'server.log' }
Comapre Method { fileDate: Wed Apr 19 2017 00:36:34 GMT-0400 (EDT),
  filename: 'server1.log' }
Comapre Method { fileDate: Wed Apr 19 2017 00:36:33 GMT-0400 (EDT),
  filename: 'server10.log' }
Comapre Method { fileDate: Wed Apr 19 2017 00:36:25 GMT-0400 (EDT),
  filename: 'server11.log' }
Comapre Method { fileDate: Wed Apr 19 2017 00:36:27 GMT-0400 (EDT),
  filename: 'server2.log' }
Comapre Method { fileDate: Wed Apr 19 2017 00:36:34 GMT-0400 (EDT),
  filename: 'server20170317143423.log' }

0 个答案:

没有答案