我正在尝试正常关闭子进程,但从未触发SIGTERM事件。
父母:
const { fork } = require('child_process');
const child = fork('./worker');
child.on('exit', (code, signal) => {
console.log(`[] Worker ${child.pid} has stopped working. Exit code: ${code}, Signal: ${signal}`);
});
process.on('SIGINT', () => {
console.log('[] Caught SIGINT: Exiting process...');
mongoose.disconnect();
});
mongoose.connection.on('disconnected', () => {
console.log('[] !!! DB Connection closed !!!');
child.kill(); // Defaults to SIGTERM (https://nodejs.org/api/child_process.html#child_process_subprocess_kill_signal)
});
孩子:
log = (msg) => {
console.log(`[PID ${process.pid}] ${msg}`);
};
process.once('SIGTERM', () => {
log('SIGTERM');
});
process.once('SIGINT', () => {
log('SIGINT');
});
process.once('exit', () => {
log('exit');
});
关闭数据库后,输出为
[] !!!数据库连接已关闭!!!
[]工作者14920已停止工作。退出代码:空,信号:SIGTERM
发送SIGINT信号时,输出为
[PID 14920] SIGINT
[]捕获SIGINT:正在退出过程...
[] !!!数据库连接已关闭!!!
[]工作者14920已停止工作。退出代码:空,信号:SIGTERM
如何在子进程中处理SIGTERM?
我正在Windows 10 Home(64位)上对其进行测试。