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.
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.
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:
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, username, app_directory, and startup_file with the correct values for your own application:
/usr/bin/flock -n /tmp/mylock.lock /home/username/nodejs/bin/node /home/username/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 that can interrupt the application and generate errors.