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:
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.
Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine.
https://nodejs.org/en/docs/guides/getting-started-guide/
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>
Advanced process manager for production Node.js applications. Load balancer, logs facility, startup script, micro service management, at a glance.
https://pm2.keymetrics.io/
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.
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
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:
#!/bin/bash
ps cax | grep node > /dev/null
if [ $? -eq 0 ]; then
echo "Process is running." >/dev/null 2>&1
else
echo "Process is not running."
PATH=$PATH:/usr/local/bin
pm2 start /path/to/your/node/application
fi
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.
Drop us a line in the comments, or get in direct contact. We’ll be eager to assist you.
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:20The 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?
Steven
25.12.2020 - 23:06I’ve added some more information in order to debug this issue in the FAQ. Does this help?
cmq
31.07.2020 - 04:06Thanks 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.
dada
22.05.2021 - 16:50install 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.