【问题标题】:Is there a way or a trigger to notify when there is an error and restart happened with PM2有没有办法或触发器在 PM2 发生错误和重启时进行通知
【发布时间】:2017-06-17 20:50:19
【问题描述】:

我有一个快速应用程序正在运行,我正在使用 PM2 来保持它的活动状态,并在系统出现任何错误时重新启动。 PM2 记录错误并重新启动,这非常好。 但我需要通知提交此错误消息的用户。是否有任何方式或事件或触发器在重新启动或将错误写入日志文件时被激活,以便我可以尝试捕获并通知实际用户?

我按照以下建议实施,

var pm2 = require('pm2');
pm2.connect(function(err) {
  if (err) {
    console.error(err);
    process.exit(2);
  }
  console.log("connected to pm2")
  pm2.launchBus(function(err, bus) {
      bus.on('log:err', function(e) {
          // Send emails
          console.log("error in pm2 send email");
      });
  });
});

我可以在 output.log 中看到“已连接到 pm2”,但在任何地方都看不到“pm2 发送电子邮件中的错误”。但是我看到了一个错误日志,我正在触发它以强制 error.log 中的错误场景。

【问题讨论】:

    标签: node.js express pm2


    【解决方案1】:

    1) 您可以使用来自Keymetrics的警报系统

    2) 可以连接到 pm2 总线并捕获错误:

    // alerts.js
    
    var pm2 = require('pm2')    
    pm2.launchBus(function(err, bus) {
        bus.on('log:err', function(e) {
            // Send emails
        });
    });
    

    【讨论】:

    • 我尝试使用您的第二个选项,我可以在 output.log 中看到打印的连接,但我无法在任何地方看到打印的 pm2 错误。但是我在error.log 中看到了我正在触发的错误日志。请在我的问题中检查上面的代码。
    猜你喜欢
    • 2020-06-05
    • 1970-01-01
    • 2021-01-05
    • 2012-03-02
    • 1970-01-01
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多