node-cron 仅不按间隔执行脚本 console.log
node-cron not executing script on interval only console.log
在初始 运行 时,它将 运行 app.js 但是,在间隔期间它会跳过它。我可能在这里做错了什么?
var cron = require('node-cron');
cron.schedule('*/10 * * * * *', function(){
var shell = require('./app.js');
commandList = [
"node app.js"
]
console.log('Bot successfully executed');
});
日志:
ec2-user:~/environment $ node forever.js
Bot successfully executed
You have already retweeted this Tweet.
You have already retweeted this Tweet.
Bot successfully executed
Bot successfully executed
Bot successfully executed
Bot successfully executed
Bot successfully executed
Bot successfully executed
Bot successfully executed
Bot successfully executed
我猜你的 app.js
立即执行代码,所以它只通过 require 执行(并且 require 被缓存,所以它只执行一次)。
您应该 return 从您的 app.js
中调用函数,并在每个 运行 上调用它。
// app.js
module.exports = () => {
// your original code here
};
// forever.js
var cron = require('node-cron');
var shell = require('./app.js');
cron.schedule('*/10 * * * * *', function(){
shell();
console.log('Bot successfully executed');
});
或者您可以在 运行ning:
之前清除 require
缓存
delete require.cache[require.resolve('./app.js')]
在初始 运行 时,它将 运行 app.js 但是,在间隔期间它会跳过它。我可能在这里做错了什么?
var cron = require('node-cron');
cron.schedule('*/10 * * * * *', function(){
var shell = require('./app.js');
commandList = [
"node app.js"
]
console.log('Bot successfully executed');
});
日志:
ec2-user:~/environment $ node forever.js
Bot successfully executed
You have already retweeted this Tweet.
You have already retweeted this Tweet.
Bot successfully executed
Bot successfully executed
Bot successfully executed
Bot successfully executed
Bot successfully executed
Bot successfully executed
Bot successfully executed
Bot successfully executed
我猜你的 app.js
立即执行代码,所以它只通过 require 执行(并且 require 被缓存,所以它只执行一次)。
您应该 return 从您的 app.js
中调用函数,并在每个 运行 上调用它。
// app.js
module.exports = () => {
// your original code here
};
// forever.js
var cron = require('node-cron');
var shell = require('./app.js');
cron.schedule('*/10 * * * * *', function(){
shell();
console.log('Bot successfully executed');
});
或者您可以在 运行ning:
之前清除require
缓存
delete require.cache[require.resolve('./app.js')]