1

I have a node.js process that kicks off a child process (via spawn). When the main process receives a request to shutdown (e.g. SIGTERM) it has to perform some clean-up before the process exits - this can take a few seconds. This clean-up relies on the child process continuing to run - however, the child process is independently responding to the SIGTERM and closing down.

Can I prevent the child process closing until the main process is ready for it to shutdown?

Thanks, Neil

Neil
  • 11
  • 1
  • 1
    I found the following thread, so will try some of the suggestions here: https://stackoverflow.com/questions/44788013/node-child-processes-how-to-intercept-signals-like-sigint – Neil Jun 08 '21 at 13:01

1 Answers1

1

After spawning child processes in detached mode, you can handle them individually. This can be of use to you: Node child processes: how to intercept signals like SIGINT.

The following assumes your child processes are detached:

process.on('SIGINT', () => {
    console.log("Intercepted SIGINT on parent");
    // Do your magic here, if you just need to wait for X time, you can use a delay promise:

    delay(5000).then(() => {
        // Kill/handle your child processes here

        process.exit(0); // Then exit the main process
    });
});

function delay(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}
EcksDy
  • 1,289
  • 9
  • 25
  • Thanks - the link to "Node child processes: how to intercept signals like SIGINT" gave me the info I needed to fix the problem. – Neil Jun 09 '21 at 09:01