Handling POSIX SIGNALS in NodeJS (Javascript!)

NodeJS, built over the v8 Engine, which is essentially C/C++ application, exposes much if not all of the POSIX signals to the Javascript layer. Hence we can have multiple options coming out of design of architecture using these SIGNALing and SIGNAL Handling possibilities. A few noteworthy things that we can think of from the top of our mind are:

  • SIGINT  Capture – A cleaner and more proper way to have a CTRL-C Handling in NodeJS Server modules/applications.
    Say , a simple code like below can do a proper clean up and exit on CTRL-C or SIGINT passed from command line / other application to the nodejs app’s ProcessID.

  • SIGUSR1 and other custom User defined SIGNALS that can be used for any custom feature implementation, say using IPC!

    So if we just keep track of a worked task’s PID we can have other application send SIGUSR1 signal (or we can do from command line using ➜kill -s SIGUSR1 pID). The worker task will interpret this signal as configuration reload indicator and can do the relevant logics for that!

Leave a Reply