Node.js server (re)starting & monitoring with Monit

1) Install monit

apt-get install monit -y

2) Config Monit for node.js

Create a monit config file for the node Upstart service

nano /etc/monit/conf.d/node.monit.conf

Append the following

check process node-app with pidfile /var/run/node.pid
    start program = "/sbin/start node" with timeout 5 seconds
    stop program  = "/sbin/stop node"
    if failed port 80 protocol HTTP
        request /
        with timeout 3 seconds
        then restart
    if cpu > 80% for 10 cycles then restart
    if 3 restarts within 10 cycles then timeout
    group server

3) Check monit config syntax

monit -t

4) Restart monit to effect changes

service monit restart

5) Check monit

monit status

6) View Monit log files

tail -n 50 /var/log/monit.log