nodejs forever detect SIGKILL or SIGINT event

Viewed 114

I launch a nodejs program on server running Ubuntu 14.04.4 LTS (GNU/Linux 3.13.0-74-generic x86_64), using forever version v0.15.2.

In my program (IoT1.js), I need to detect when it is killed (for another launch) so that I can write the last states of the connected clients. The simplified version of my program:

const WebsocketServer = require("websocket").server
... // websocket listener

const jsonfile = require("jsonfile")
...

process.on('SIGINT', () => { // also tried SIGKILL, not working either
  console.log("KILL fired")
  jsonfile.writeFileSync("./someLastState.json", states)

  // process.exit(-1)
})

Then from ssh terminal, I tried these combinations:

forever start -p ./ -l lIoT1.log -e eIoT1.log -c node IoT1.js
forever stop <pid>

forever start -p ./ -l lIoT1.log -e eIoT1.log -c node IoT1.js
forever stop <pid> --killSignal=SIGINT // or SIGKILL

forever start -p ./ -l lIoT1.log -e eIoT1.log -c node IoT1.js --killSignal=SIGINT
forever stop <pid>

forever start -p ./ -l lIoT1.log -e eIoT1.log -c node IoT1.js --killSignal=SIGINT
forever stop <pid> --killSignal=SIGINT

but none works! i.e. "KILL fired" is not printed (I used tail -f lIoT1.log to continuously read its output) and someLastStates.json is not created.

What should I do to catch this KILL signal, so that my nodejs has the last chance to write down some file before actually killed?

Regards

0 Answers
Related