简体   繁体   English

如何在 Node.js 中捕获 * 并忽略 * SIGINT?

[英]How do I catch *and ignore* SIGINT in Node.js?

I've found a different post on a related topic ( How to perform an async operation on exit ), but perhaps it doesn't apply to macOS, or just isn't true anymore when running Node.js v14.我找到了一篇关于相关主题的不同帖子( 如何在退出时执行异步操作),但它可能不适用于 macOS,或者在运行 Node.js v14 时不再适用。

My original question was more complicated.我原来的问题更复杂。 I couldn't get setTimeout s or await to work correctly in a callback (or async callback) for SIGINT.我无法在 SIGINT 的回调(或异步回调)中获得setTimeoutawait以正常工作。 But then I realized what I really needed to do, first and foremost, was to be able to catch and ignore SIGINT.但后来我意识到我真正需要做的是,首先也是最重要的是能够捕获并忽略SIGINT。

If I could do that, then I could shutdown in response to SIGINT too, if and when I wanted to.如果我能做到这一点,那么我也可以在需要时关闭以响应 SIGINT。

So I went back to basics to see if I could simply disable SIGINT.所以我回到基础,看看我是否可以简单地禁用 SIGINT。 I couldn't.我做不到。

I could detect SIGINT, I could respond with console messages or any other synchronous code, but my code was going to shutdown, and shutdown soon, no matter what I did.我可以检测到 SIGINT,我可以用控制台消息或任何其他同步代码进行响应,但是无论我做什么,我的代码都会关闭,并且很快就会关闭。

process.on('SIGINT', () => { console.log('here'); return false; });
process.on('SIGINT', () => { console.log('here'); return true; });
process.on('SIGINT', () => { console.log('here'); return undefined; });
process.on('SIGINT', () => { console.log('here'); return new Promise(resolve => { console.log(resolve); }); });

None of these approaches prevents my Node.js server from shutting down after hitting Ctrl + C , or otherwise sending a SIGINT to the process.这些方法都不能阻止我的 Node.js 服务器在按下 Ctrl + C后关闭,或者以其他方式向进程发送 SIGINT。

UPDATE:更新:

When I run my server via node app.js , SIGINT interception is working just like it should!当我通过node app.js运行我的服务器时,SIGINT 拦截工作正常!

Apparently I've been being driven crazy by an artifact of running my server app indirectly through nodemon .显然,通过nodemon间接运行我的服务器应用程序的神器让我发疯了。 A Ctrl + C from my IDE's terminal, or an interrupt sent using the IDE's graphical red (stop) button can't be properly caught, because the resulting signal is really being sent to nodemon.来自我的 IDE 终端的Ctrl + C或使用 IDE 的图形红色(停止)按钮发送的中断无法被正确捕获,因为结果信号确实被发送到 nodemon。 Outside of the IDE environment, however, without nodemon figuring into things, my graceful shutdown works as I want it to.然而,在 IDE 环境之外,如果没有 nodemon 解决问题,我的正常关机会按我想要的方式工作。

If you observe this kind of unexpected shutdown behavior always think "who is sending what signal?".如果您观察到这种意外的关机行为,请始终思考“谁在发送什么信号?”。 Depending on the OS and other processes a lot of other signals could be sent, for example see How does SIGINT relate to the other termination signals such as SIGTERM, SIGQUIT and SIGKILL?根据操作系统和其他进程的不同,可能会发送许多其他信号,例如,请参阅How does SIGINT related to the other termination signals such as SIGTERM, SIGQUIT and SIGKILL? . .

What was most likely happening in your case was that the nodemon process was sending a SIGKILL after the SIGINT .在您的情况下最有可能发生的是nodemon进程在SIGINT之后发送SIGKILL Several such signals exist that cannot be aborted.存在多个无法中止的此类信号。 nodemon allows to control the default termination signal via --signal , seehttps://github.com/remy/nodemon/blob/master/doc/cli/options.txt . nodemon允许通过--signal控制默认终止信号,参见https://github.com/remy/nodemon/blob/master/doc/cli/options.txt nodemon can also be configured via a local and global config file, see https://github.com/remy/nodemon/blob/master/doc/cli/config.txt . nodemon也可以通过本地和全局配置文件进行配置,请参阅https://github.com/remy/nodemon/blob/master/doc/cli/config.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM