Score:0

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

br flag

Hopefully this question has an answer that's generic to Apache and Node.js, and not specific to Plesk ('cause I know how you feel about Plesk-specific questions!).

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 server platform, 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 (cough) 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?

Michael Hampton avatar
cz flag
Have you checked your app's log?
kshetline avatar
br flag
There's nothing helpful there I can see, unless I don't know the right logs to look at. In the Apache logs, I see nothing. In `/var/log/httpd/error_log`, where all Node.js output goes (whether it's from stdout or stderr), all I see is my Node.js running merrily along, then restarting without a hint as to why it needed to restart.
Michael Hampton avatar
cz flag
I think there's very little at this point that we can do. You'll need to dig into whatever Plesk is doing, either when you click that restart button or whatever it does at 3:30 am, most likely both.
kshetline avatar
br flag
I wasn't sure if this problem was Plesk specific, or if there might be a standard implementation for using Node.js with Apache, and general Apache-related advice. I posted a few days ago talk.plesk.com, and so far... crickets!
kshetline avatar
br flag
When I entered 'plesk' as a tag for this post, there was a warning for that tag that most questions about Plesk belonged on webmasters.stackexchange.com. This same question there, however, got closed as off-topic, so I hope someone can help here. The Plesk forum is dead quiet on the matter.
Michael Hampton avatar
cz flag
There's no standard way to start a Node.js app. You'll have to find out how Plesk does it, and that's not something we can help with.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.