5

I have a simple shell script written in Node.js and I'm trying to do some cleanup before the process exits (⌃C). I've written the following code but for some reason the cleanup function doesn't complete. Any ideas why?

if (keepAlive) {

   process.stdin.resume();

    ['exit', 'SIGINT'].forEach(function(signal) {

        process.on(signal, function() {

        console.log("Received Signal: '" + signal + "'. Cleaning up...");

        cleanup(function(err) {
          console.log('this function is called but does not finish')
          process.exit();
        });

      });

    });
else {
    process.exit();
}

I'm using Node v6.9.2

5
  • what do you mean by doesn't complete? Commented Jan 24, 2018 at 23:09
  • What platform are you running on? Some SIGINT behavior is platform-specific. Commented Jan 24, 2018 at 23:09
  • @Daniel the cleanup function is called but the process exits before it's done. Commented Jan 24, 2018 at 23:25
  • @jfriend00 I'm on OSX 10.13.2 Commented Jan 24, 2018 at 23:26
  • This had to do with a node module I was using Inquirer.js. This was the issue: github.com/SBoudrias/Inquirer.js/issues/293. I upgraded the module and now everything is working fine. Commented Jan 25, 2018 at 2:00

2 Answers 2

8

You can do this :

process.on("SIGTERM", () => {
    ayncFunction().finally(() => process.exit(0));
});

process.on("SIGINT", () => {
    ayncFunction().finally(() => process.exit(0));
});

process.on("exit", code => {
    console.log(`APP stopped !`);
});
Sign up to request clarification or add additional context in comments.

Comments

5

You can't reliably run async code from process.on('exit'). Here's a quote from the doc:

There is no way to prevent the exiting of the event loop at this point, and once all 'exit' listeners have finished running the Node.js process will terminate.

So, as soon as the exit listener returns (even though it is still trying to finish up it's async processing), then the process will exit.


For a SIGINT handler, you should be able to do anything you want because once there is a SIGINT handler registered, there is no default behavior to exit the process so the process will only exit when you tell it to. From the doc:

If one of these signals [SIGINT or SIGTERM] has a listener installed, its default behavior will be removed (Node.js will no longer exit).

This means you should be able to exit when your async processing is done if you write your async handling code properly (which you do not show in your question).

2 Comments

From what I can tell you're saying I should use SIGINT which is what I'm doing. Am I missing something?
@jwerre - Yes, for non-Windows platforms, it SIGINT should work fine for Ctrl-C. See stackoverflow.com/questions/20165605/detecting-ctrlc-in-node-js for details. It also may be that your async code is not properly waiting for completion of the async operation before you call process.exit(). If you show us that async code, we could advise on that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.