How to create persistent Node.js applications on shared and reseller hosting accounts

As part of A2 Hosting Perpetual Security, long running processes are periodically terminated on our cPanel Shared and Reseller servers. This includes node.js applications. Learn how to keep your node.js application up and running with this article.

Table of Contents

Preferred method

The preferred method for making persistent node.js applications on cPanel Shared and Reseller servers is to use the Node.js selector. Learn to setup a node.js application using the Node.js selector with this article.

Cron jobs

The following instructions assume you have installed node.js according to this article.

Sometimes an application must be run on a specific port and the Node.js selector cannot be used. Popular methods of creating persistent node.js applications like PM2 and Forever are also processes. They are subject to periodic termination and so cannot be relied on to be available to restart node if it should be terminated.

Cron jobs run periodically and are not terminated and so can be counted on to restart a terminated node.js process. Follow thses steps to create a cron job to restart your node application:

  1. Log in to cPanel.
    If you do not know how to log in to your cPanel account, please see this article.
  2. In the ADVANCED section of the cPanel home screen, click Cron Jobs:

    cPanel - Advanced - Cron Jobs icon

  3. Under Cron Email, type the e-mail address where you want to receive notifications, and then click Update Email. Every time the cron job runs, the e-mail account receives a message.

    If you do not want to receive e-mail notifications for a particular cron job, you can append >/dev/null 2>&1 to the cron job command, which redirects all output to /dev/null.
  4. Under Add New Cron Job, specify the interval for the command you want. For this application we want the cron to run as often as possible to insure maximum uptime for the node.js application. In the Minutes text box type */15 or select Once per Fifteen Minutes(*/15) from the Minutes list box. No other time settings are required.
  5. In the Command text box, type the following command, replacing mylock, app_directory, and startup_file with the correct values for your own application:

    /usr/bin/flock -n /tmp/mylock.lock ${HOME}/nodejs/bin/node ${HOME}/app_directory/startup_file.js

    The flock command used in this example checks to see if the node instance is still running before attempting to start a new instance. As long as the original process is still running, a new one is not started. This avoids port contention issues that can interrupt the application and generate errors.

  6. Click Add New Cron Job. cPanel creates the cron job.

Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.

We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.