PM2 Catching Errored State Signal

爷,独闯天下 提交于 2021-01-07 02:42:25

问题


I am trying to catch the process before it goes into the state of errored. The process I am running is erroring and restarting correctly. After 15 attempts of restarting it will go into a state of errored, as shown for the process with an ID of 0 below.

┌─────┬─────────────────────────────────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name                                    │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼─────────────────────────────────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 1   │ a58a1e0d-3a6f-4512-8b83-4dcfd2f9e408    │ default     │ 1.0.0   │ fork    │ 3139     │ 8s     │ 0    │ online    │ 0%       │ 61.6mb   │ warren   │ disabled │
│ 0   │ e95ff617-4800-4059-906b-2cde63bcb4b6    │ default     │ 1.0.0   │ fork    │ 0        │ 0      │ 15   │ errored   │ 0%       │ 0b       │ warren   │ disabled │
└─────┴─────────────────────────────────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

Before it goes into a state of errored what signal (if any) is sent to the process?

For example when I issue a pm2 stop <PROCESS_NAME> I can can intercept the SIGINT message and log something to my log file as in the example below.

process.on('SIGINT', function() {
  logger.info("I HAVE BEEN KILLED")
})

I need something like this but the signal sent to the process when it switches to an errored state is listened for.


回答1:


You can just use the local library of pm2 to catch the state of errored processes from logs.

  1. npm install pm2 on your project directory

  2. Go into the file where process is initiated

  3. Add the following command into the file, it will add an event listener your process and will catch if error occured

    const pm2 = require('pm2')    
    pm2.launchBus(function(err, bus) {
        bus.on('log:err', function(e) {
            //When a task throws errors
        });
    });
    


来源:https://stackoverflow.com/questions/64910872/pm2-catching-errored-state-signal

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