Basic Shell script to automatically start NodeJS if for some reason it stopped

Hi Guys,

Well this one is a really basic example of a shell script that I used before in one of my production NodeJS website called http://icompressjs.com

Here is the bash script that I wrote to monitor my local node install

 

 

as you can see that I am running Node on port 8080 and I am using curl to send a request to my node server.

If response is 200 that is good and it will print “All Working well

else it will try to start NodeJS server

You can also plugin another line to send email to you when a server is launched again.

Something like this

put above line just under echo “Launching”

Now the final step is to cron this our bash script. I named it as node_monitor.sh

You will have to give your local user rights to execute this script, after doing that you are able to cron it as shown below

Above lines says that every 5th minute run node_monitor.sh

If the response is ok i.e. 200 then nothing will be done else there will be an attempt to start NodeJS server again

You can improve this script with better error handling but for what I was trying to do this was sort of enough

I hope this helps

Cheers

 

 

 

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.