How do I get a graceful shutdown of Node.js running on Apache/CentOS/Plesk?

I recently decided to add some text message notifications to a Node.js server app, for when a particular update was discovered. While I was at it, I figured it would be a good idea to add a server start-up and shutdown notifications too. This led to a few of unpleasant discoveries:

  1. Roughly every 24 hours my server (not the entire Plesk server, just this one Node.js server app) was restarting for no good reason, between 3:30-4:30 AM.
  2. There was no graceful detection of a shutdown before each restart, so it appears that the Node.js app was being killed outright, not sent SIGINT or SIGTERM, which I was listening for.
  3. Even using the Plesk control panel, where you can request an app restart, a graceful restart doesn’t occur. The Node.js app is simply unceremoniously killed.

Node.js Restart App button

The once-per-24-hour restart, after some investigation, is apparently triggered by Apache log rotation. Why simple log rotation should cause this is another mystery to be solved.

In the meantime, I want to know if there’s a way to make sure GRACEFUL shutdowns happen, whether they happen because Apache is stopping or shutting down, or because the "Restart App" button is pressed.

I look for shutdowns by monitoring these signals:

process.on('SIGINT', shutdown); process.on('SIGTERM', shutdown); process.on('SIGUSR2', shutdown); // Used by my dev tools 

Is there a different signal I should check for? Is the process shutdown mercilessly with a SIGKILL instead? If so, can I change that, perhaps with some sort of delay to give a graceful shutdown some time to occur?