Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.0k views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...