Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 694
Bala Sivagnanam Avatar asked Feb 01 '17 07:02

Bala Sivagnanam


1 Answers

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
    });
});
like image 177
stdob-- Avatar answered Sep 16 '22 20:09

stdob--