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

As part of A2 Hosting Perpetual Security initiative, long-running processes are periodically terminated on cPanel Shared and Reseller servers. This includes Node.js applications. This article discusses two ways to keep your Node.js application up and running:

  • Use the Node.js Selector in cPanel: This is the preferred method.
  • Use a cron job: You only need to set up a cron job if you have manually created a Node.js application (in other words, you did not use the Node.js Selector in cPanel).

Method #1: Use the Node.js Selector in cPanel

This is the preferred method for maintaining persistent Node.js applications on cPanel shared and reseller servers. The Node.js Selector maintains the running state of Node.js applications, with an easy-to-use interface that you can use to stop, start, and restart applications.

For information about how to use the Node.js Selector in cPanel, please see this article.

Method #2: Use a cron job

The following procedure assumes that you have already installed Node.js according to this article.

If you need to run an application on a specific port, then you cannot use the Node.js Selector and must manually create the application instead. Popular methods of creating persistent Node.js applications like PM2 and Forever, however, will not work. They are also running processes, and are subject to periodic termination.

Cron jobs, however, run periodically and are not terminated, so you can rely on them to restart a terminated Node.js process. To set up a cron job to restart your application, follow these steps:

  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.