Eric Bergman-Terrell's Blog

Node.js Programming Tip: Keep your Web App Running
September 20, 2015

I've mentioned in a earlier blog post that this website uses the forever module to ensure that it keeps running, even in the event of an uncaught exception causing the entire app to halt. However, a Node.js application can still stop working, even if an exception is never thrown. For instance, the app could have an infinite loop, or experience a deadlock. In this case, forever will not help. The forever module will only restart the app if the app's process dies.

Fortunately for me, this website is hosted on a Linux server, where detecting and restarting an unresponsive app is easy. My solution is as follows:

  1. Create one or more "alive" urls that are used to determine if the web app is alive and responsive.
  2. Create a scheduled job that verifies that the website is alive every N seconds, and restarts it if not.

My "alive" url just returns the current date/time and an HTTP status code of 200 if the app is running. It also logs some interesting memory usage statistics, so that I can be aware of any memory leaks that might occur.

    app.get('/alive', function(req, res) {
        var message = 'alive at ' + new Date().toISOString();;'rss: ' + utils.formatNumber(process.memoryUsage().rss));'heapTotal: ' + utils.formatNumber(process.memoryUsage().heapTotal));'heapUsed: ' + utils.formatNumber(process.memoryUsage().heapUsed));


I wrote a bash shell script, named "keep_alive" that determines if the app is alive. keep_alive writes to a log having a filename corresponding to the current date. This log will be useful, since it will allow me to troubleshoot website failures in the future. keep_alive uses the Linux "curl" command to hit the alive url and return the status. The curl command is set to timeout after 15 seconds. The "urls" array holds all the alive urls. If you need to monitor multiple urls, just add more elements. If any of the alive urls do not yield good responses within the timeout, the app will be restarted, by calling the "bounce" shell script.



. ~/.bashrc

pushd /home/ericbtco

log="logs/keep_alive/`date +\"%Y-%m-%d\"`.log"


for url in "${urls[@]}"

  echo -n checking $url at `date`: >> $log

  if curl -X GET --max-time 15 -I $url | grep -q "200 OK"; then
    echo " alive" >> $log
    echo " **** DEAD ****" >> $log



if $bounce; then
  echo "**** BOUNCING ****" >> $log
  . ./bounce >> $log

echo "" >> $log


The bounce shell script restarts the app by first stopping the process with the "forever stopall" command. The app may be running but unresponsive, so the script does not assume that the process has halted. Then the script runs the "run_ericbt" shell script to re-launch the app.



. ~/.bashrc

pushd /home/ericbtco

forever stopall

echo "restarting"
bash ./run_ericbt

echo "running jobs"

forever list


The Linux cron command runs the keep_alive script once a minute. Here's the crontab entry:

*/1 * * * * /home/ericbtco/keep_alive

In addition to restarting the app when it becomes unresponsive, the app will also be restarted after a server reboot. About one minute after the reboot, cron will run "keep_running". The app will not respond, and will be restarted.

use cron and curl to monitor and restart your node.js app

Keywords: Node.js, node, Express, forever, cron, Linux, deadlock, Linux, bash, shell script, curl, hung, memory leak, rss

Reader Comments

Comment on this Blog Post

Recent Posts

Python Script to Audit MediaMonkey TranscodingAugust 15, 2019
How to decompile Java code with JetBrains IntelliJ IDEA (2018.2.3, Windows 10)October 5, 2018
Java Programming Tip: SWT Photo Frame ProgramOctober 31, 2016
Vault 3 (Desktop) Version 1.63 ReleasedSeptember 9, 2016
"Compliance with Court Orders Act of 2016"April 9, 2016
Disable "Visual Voicemail" on Android / T-MobileJanuary 17, 2016
IPv6 HumorDecember 10, 2015