繁体   English   中英

为什么在我的代码示例中没有调用 SIGTERM 事件处理程序?

[英]Why SIGTERM event handler wasn't called in my code example?

视窗 10 x64
节点 v12.19.0

为什么在浏览器中首次打开http://localhost:3000/后没有调用我的SIGTERM事件处理程序? 应用程序在没有执行我的处理程序的情况下终止(我没有看到它的控制台输出)。

// node v12.19.0
const http = require("http");
const server = http.createServer((req,res)=> {
    res.statusCode = 200;
    res.end("Hello, World!");
    setTimeout(() => { process.kill(process.pid, "SIGTERM"); } , 2000);
});

process.on("SIGTERM", () => { // Why it will not be executed?
    process.statusCode = 1;
    console.log("SIGTERM");
    server.close(() => {
        console.log("Process terminated.");
    });
});

server.listen(3000,()=>{
    console.log("Server works on http://localhost:3000/");  
});

我的控制台输出(PowerShell):

PS C:\tmp_src> node index.js
Server works on http://localhost:3000/
PS C:\tmp_src>

在 Windows 10 上, process.on('SIGINT',cb)对我process.on('SIGINT',cb) ,可以在 shell 中捕获中断,如CTRL + C ,或在 IDE 中停止按钮命令。

它也适用于 Linux 上的生产,特别是对于pm2 restart和类似命令。

暂无
暂无

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

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