If you don’t want to use external tools to ensure your node server is still up and running, you may just use a super simple bash script to make sure the server gets restarted whenever it failed.
Just create a bash script in a location of your choice and give it permissions of the user you are starting your node server with. I will use the home folder of my user for node.js, which is called ‘unode’:
mkdir /home/unode/bashscripts/ nano node_restart.sh
Insert the following lines, replace the path to your node main app file and log files:
#!/bin/sh NODE_MAIN="node /var/www/node/app.js“ if [ -z `pgrep -f -x "$NODE_MAIN"` ] then echo "Restarting $NODE_MAIN." cmdNODE="$NODE_MAIN 1> /var/log/node/node.log 2> /var/log/node/error.log &" eval $cmdNODE fi
Save the file and set permissions to allow the bash script to be executed by cron:
chmod 755 /home/unode/bashscripts/node_restart.sh
Finally add a one minute cron to your crontab file and you are set:
*/1 * * * * unode /home/unode/bashscripts/node_restart.sh
Now your node.js app will be monitored once a minute and restarted if it stopped. Of course you can modify the script very easily to monitor and restart all kinds of other processes as well.