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

∥☆過路亽.° 提交于 2021-01-27 19:21:19

问题


Windows 10 x64
Node v12.19.0

Why wasn't called my handler of SIGTERM event after first opening http://localhost:3000/ in a browser? Application is terminated without executiong my handler (I don't see its console output).

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

My console output (PowerShell):

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

回答1:


On Windows 10, process.on('SIGINT',cb) works for me, to catch interrupts like CTRL+C in a shell, or stop button commands in an IDE.

It also works well in production on Linux, particularly for pm2 restart and similar commands.



来源:https://stackoverflow.com/questions/64390685/why-sigterm-event-handler-wasnt-called-in-my-code-example

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