看到有个node-schedule,那我要跑定时任务时就在服务器里起一个node进程,一直跑着下面的进程就好了?
koa2和express之后的课有没有更方便的方法?
const schedule = require('node-schedule');
const path = require('path')
const fs = require('fs')
const source = path.join(__dirname,'../','../','logs','access.log')
function createDistPath(){
const year = new Date().getFullYear()
const month = new Date().getMonth() + 1
const day = new Date().getDate()
const hour = new Date().getHours()
const min = new Date().getMinutes()
const sec = new Date().getSeconds()
return path.join(__dirname,'../','../','logs',`${year}-${month}-${day}-${hour}-${min}-${sec}.access.log`)
}
let job = schedule.scheduleJob('40 * * * * *', () => {
const dest = createDistPath()
fs.copyFileSync(source,dest)
// fs.renameSync(source,dest);//不删测试
console.log('生成了日志',dest)
});