PM2 捕捉错误状态信号 [英] PM2 Catching Errored State Signal

查看:116
本文介绍了PM2 捕捉错误状态信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在进程进入错误状态之前捕获进程.我正在运行的进程出错并正确重新启动.尝试重新启动 15 次后,它将进入错误状态,如下面 ID 为 0 的进程所示.

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?

例如,当我发出 pm2 stop 时,我可以拦截 SIGINT 消息并将某些内容记录到我的日志文件中,如下例所示.

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.

推荐答案

你可以直接使用 pm2 从日志中捕获错误进程的状态.

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

  1. npm install pm2 到你的项目目录

  1. npm install pm2 on your project directory

进入启动进程的文件

将以下命令添加到文件中,它将为您的进程添加一个事件侦听器,并在发生错误时进行捕获

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

这篇关于PM2 捕捉错误状态信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆