在初始运行时,它会运行 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 被缓存,所以它只执行一次)。
您应该改为从 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');
});
或者您可以在运行前清除require
缓存:
删除 require.cache[require.resolve('./app.js')]
关于javascript - node-cron 仅在 console.log 间隔内不执行脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50050424/