Cart (0)
  • Your cart is currently empty.

NEOSERV BLOG

Tips, guides and useful information about domains, hosting, SSL certificates, email, web optimization and online security.

Category: Web Hosting
Published:

Our shared web hosting packages (starting with the GREEN package) also allow you to develop and run Node.js applications. But developing a Node.js application is only the first step, you also need to make sure that the application is running all the time.

In the following, you will learn how to use the PM2 process manager to make the application restart automatically in case of an unexpected error or shutdown.

Follow these steps:

  • Write reliable Node.js code with best practices for error handling.
  • Using PM2 Process Manager, detect the error and restart the application.
  • Configure a cron job to check the application is running and restart PM2 if the application fails.

Using PM2 Process Manager

PM2 is a process manager for Node.js applications. It is used to control and automatically restart applications – in case an unforeseen event causes the application to shut down or become inoperable.

To install the PM2 process manager, use the following command:

npm install pm2

To start the application, use the command:

pm2 start myapp.js

Once you have started the applications in this way (you can start several applications), you can check the list of all active applications:

pm2 list

In addition, you can also check the details of each active application using PM2:

pm2 show <app-id>

But what happens when the shared server is restarted (e.g. when the server is updated)? Then the PM2 process is stopped and has to be restarted. The same applies to the application. You will achieve that the PM2 process manager will start automatically after the server restarts with a cron job.

Setting up a cron job to automatically restart PM2

Cron jobs allow you to automate the launching of applications or scripts at a specific time. This time can be defined as a time interval (e.g. every hour) or as a system event (e.g. server restart).

A properly configured cron job will therefore perform two functions:

  • It will check the status of the application at defined time intervals and on server restart.
  • It will restart the PM2 process manager if it is found to be inactive.

By using a cron job, you will ensure that the downtime of your Node.js application is minimised on the shared web hosting server.

Start by preparing a script that will check the execution status of your Node.js application:

#!/bin/bash
ps cax | grep node > /dev/null
if [ $? -eq 0 ]; then
echo "The Node.js application is active." >/dev/null 2>&1
else
echo "Node.js application is not active."
PATH=$PATH:/usr/local/bin
pm2 start /path/to/your/node/application
fi

The above code will check if the node process is running, and if not, it will restart it using the PM2 process manager.

Now add the periodic execution of this code:

  • by adding a cron job via cPanel: sh /path/to/your/script
  • by editing the cron job directly from the terminal: crontab -e

By implementing the steps described above, you will achieve that your Node.js application on shared hosting will be active for the whole time the server is running. In case of an unexpected error, PM2 will detect it and restart the application. In the event of a server failure or restart, the script – in combination with a cron job – will automatically restart the application.

Did you know that you can enter your email address in the cPanel dashboard and a message will be sent to you when the cron job is successfully executed? This way you will receive a notification when the application is down or restarted.

COMMENTS

COMMENT THE POST

(mandatory)
(mandatory, email address will be hidden)
(optional)
Security question that confirms you are a real person.