Is there a way or a trigger to notify when there is an error and restart happened with PM2

筅森魡賤 提交于 2020-02-02 03:50:08

问题


I have an express application running and I am using PM2 to keep it alive and restart when there is any error in the system. PM2 logs the error and restarts which is perfectly fine. But I need to notify the user who submitted this with the error message. Is there any way or event or trigger which is activated when the restart happens or when the error is being written to the log file so that I can try to capture that and notify the actual user?

I implemented as per suggestion below,

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");
      });
  });
});

I can see "connected to pm2" printed in output.log but I can't see "error in pm2 send email" printed anywhere. But I see an error log that I am triggering to force the error scenario in error.log.


回答1:


1) You can use alert system from Keymetrics

2) You can connect to pm2 bus and catch errors:

// alerts.js

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


来源:https://stackoverflow.com/questions/41974159/is-there-a-way-or-a-trigger-to-notify-when-there-is-an-error-and-restart-happene

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!