1

I was just trying to work with signals like SIGTERM and SIGINT. The idea is I want to send SIGTERM signal with process.kill(process.pid, "SIGTERM") when something happens and then handle that signal with process.on("SIGTERM", callback).

My expectation is that when process.kill(process.pid, "SIGTERM") runs, the SIGTERM handler should run the callback function and do some cleanup work.

But this does not happen and process exits without running the callback function.

if (condition) {
  process.kill(process.pid, 'SIGTERM');  
}

process.on('SIGTERM', () => {
  console.log('SIGTERM SIGNAL RECEIVED');
});
Mubashir Hassan
  • 199
  • 1
  • 7
  • Does this answer your question? [What is the Windows equivalent of process.on('SIGINT') in node.js?](https://stackoverflow.com/questions/10021373/what-is-the-windows-equivalent-of-process-onsigint-in-node-js) – vallentin Aug 04 '20 at 07:13
  • What OS? It's probably a Windows issue. I tried on Windows and I too didn't get the SIGTERM log, but weirdly I tried it on [WSL](https://learn.microsoft.com/en-us/windows/wsl/about) (Linux on Windows) on which it worked expected. Wouldn't be surprizing to me as Node.JS on Windows has a lot of such oddities. You may find more info on Node Github issues, such as this: https://github.com/nodejs/node/issues/12378 – laggingreflex Aug 04 '20 at 11:18
  • @vallentin that question is talking about **SIGINT** signal which is triggered when we hit CTRL + C from the terminal. I am asking about the "SIGTERM" signal and also i want to to trigger these signal from within my code. Not from terminal. – Mubashir Hassan Aug 05 '20 at 08:23
  • @laggingreflex yes i tried these signals on linux and they work perfectly fine. But not on windows. – Mubashir Hassan Aug 05 '20 at 08:27
  • @MubashirHassan Yes, I know. But the question contains related info. If I had to give an answer, I'd say "Signals is a POSIX thing, so don't expect them to work on Windows". – vallentin Aug 05 '20 at 08:28
  • @vallentin yes i noticed it. it doesn't work on windows. – Mubashir Hassan Aug 10 '20 at 16:28

0 Answers0