Search code examples
node.jssolariskillsigtermshutdown-hook

Node.JS Shutdown Hook


Is it possible to intercept the default kill signal and use it as a command for a graceful shutdown? This is for Solaris SMF. The easiest way to have a stoppable service that I have found is to set :kill as the shutdown script and then to add a shutdown hook in Java. In this case, I want to do it for Node.JS. How should I do it?

Edit: The purpose is to

  1. Stop receiving new requests.
  2. Give existing callbacks a few seconds to finish.
  3. Write some information to stderr.

@alienhard's first suggestion was to use process.on('exit'... but it seems that I would not be able to accomplish number 2 with this method.


Solution

  • The only thing that comes to my mind is using signal events.

    http://nodejs.org/docs/v0.3.1/api/process.html#signal_Events