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:
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.
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:
Under Cron Email, type the e-mail address where you want to receive notifications, and then click . Every time the cron job runs, the e-mail account receives a message.
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.
Subscribe to receive weekly cutting edge tips, strategies, and news you need to grow your web business.
No charge. Unsubscribe anytime.
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.