javascript - node-cron 仅在 console.log 间隔内不执行脚本

标签 javascript node.js bots node-cron

在初始运行时,它会运行 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/

相关文章:

javascript - 当没有数据时 document.write 停止工作

javascript - 从区域时间获取 utc 时间 moment.js

javascript - 按值而不是引用将数组传递到数组数组中

Node.js - 我不断收到以下错误 : Error: ffmpeg stream: write EPIPE

node.js - 使用 node.js 查找 Heroku 自定义域

javascript - 在运行时创建表jquery上使用自定义插件

javascript - socket.io CORS access-control-allowed-origin 设置为 *

javascript - 使用 JavaScript 生成的复选框防止垃圾邮件和机器人程序

javascript - 如何使我的命令不提及用户?

ruby - 使用 Mechanize 按标签选择表单字段?