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

1

9

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.

Fimbria answered 1/2, 2017 at 7:27 Comment(0)
C
4

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
    });
});
Confiding answered 1/2, 2017 at 9:9 Comment(1)
I tried to use your second option, I can see the connection printed in output.log but I cant see error in pm2 printed anywhere. But I see an error log that I am triggering in error.log. Please check the code above in my question.Fimbria

© 2022 - 2024 — McMap. All rights reserved.