Discover 3 steps to keep node.js running on shared hosting

Steven     02.08.2020

Building a node.js application is one thing. Making sure it is always operational is another. This article details a simple approach to keep your node.js application online on shared hosting. These are the three steps to follow:

  1. Write robust node.js code with failure and error handling routines
  2. Use PM2 process manager to catch and restart unexpected failure
  3. Configure crontab on shared hosting to periodically check and restart

Write robust node.js code

First things first. Start by writing your code the recommended node.js way. That means, use asynchronous calls for functions that can take some time. Also, catch error situations where possible. When you do experience your application halting, find out why. Then catch the situation by code, if possible.

Use PM2 process manager

PM2 is a production process manager for node.js. PM2 monitors your application and automatically restarts it when an unexpected event made it crash. Install PM2 using

npm install pm2

and use it to start your application

pm2 start myapp.js

Once started like this, PM2 lists all running applications (yes, you can give it multiple applications if you like!) and shows you details of each of them

pm2 list

pm2 show <app-id>

So far so good. Now what happens when the shared hosting server running your application reboots or, even worse, crashes? Right, the PM2 process is killed and will not be restarted automatically. The same goes for your application.

In order to handle that situation as well, a third measure is needed.

Configure crontab on shared hosting

A crontab you can see as a list of commands that need to be run at specified times. These times can either be fixed intervals (let’s say eg every 5 minutes), or system events (eg shared hosting server reboot). 

Crontab is used here to

  • check periodically and at server reboot the status of the application, and 
  • restart the PM2 process in case it was found dead

This way, downtime of the node.js application on shared hosting is minimized. Start by writing a script that verifies the running state of your application:

ps cax | grep node > /dev/null
if [ $? -eq 0 ]; then
  echo "Process is running." >/dev/null 2>&1
  echo "Process is not running."
  pm2 start /path/to/your/node/application

This code will check if a process named ‘node’ is running and, if not, restarts it using pm2. Now add periodic execution of this code either using cPanel as a Cron Job running every 5 minutes:

sh /path/to/your/script

Or edit crontab directly from the terminal to add the same line:

crontab -e

By implementing these three measures, we make sure our node.js application keeps running on shared hosting. In case of unexpected failure, PM2 will catch it and restart. In case of server downtime or reboot, a custom script running periodically catches the situation and restarts the application automatically.

On top of this all, you can configure cPanel to send emails when a cron job produces output. This way, you get a notification when the application is down and being restarted.

Still not working?

Drop us a line in the comments, or get in direct contact. We’ll be eager to assist you.

Frequently Asked Questions

Why does my node.js server stop after closing the terminal?

In order to debug this, it can be useful to find out whether this is a node.js or a PM2 issue. Can you try to launch another process (e.g. VIM editor) and check if this keeps running when closing the terminal?
Alternatively, the node.js server might not be well started with PM2. For this, you can list all active node processes (see example script) and make sure only one is active after running the PM2 command.

Yoenis Pantoja Zaldívar
12.08.2020 - 22:20

The same thing happens to the user “cmq”. When closing the session or closing the terminal the node falls and the pm2 does not lift it. Any ideas or help?

25.12.2020 - 23:06

I’ve added some more information in order to debug this issue in the FAQ. Does this help?

31.07.2020 - 04:06

Thanks for this post.

For some reason this didn’t work for me. All the parts are fine:
I can pm2 the node server to run, and it stays running indefinitely — as long as the ssh session remains open.
The moment you exit the ssh session, the node server shuts down and pm2 doesn’t restart it (in other words, ssh back in immediately after exiting, run pm2 list, and the node server is no longer running)
Running the .sh file triggers pm2 to run the server again, no problem, but again, exiting terminal kills it.
And for some reason the crontab has no effect whatsoever.

22.05.2021 - 16:50

install npm in your server and type this
`npm install -g forever`
then type
`forever start cd file/location/server.js`

This will run even after the closing of the SSH session. I use crontab to run the forever command to run when the server is restarted.
`crontab -e`
inside vim or any file editor (go to the last line) type this,
`@reboot sh -c ‘cd file/location && forever start server.js’`

This works for me.