Restart a Node App via CRON Script

,

There are times that you are needing to run a cron job within a websites control panel that ensures the websites node app remains active if it stops for any reason.  Here is the little script that I used where the executables for node where within the bin directory under the public_html directory.  I found I had to CD into the public_html directory probably because of some paths within the application not being absolute and being relative.  So here was the code that I put into a check_node.sh file within the root path for that user. Then I modified the crontab and had the system fire this every 5 minutes or so.

#!/bin/bash

  if (ps aux | grep 'node app.js' | grep -v grep > /dev/null)
  then
      echo RUNNING
  else
      echo STOPPED
        cd public_html/
        bin/node app.js &
  fi

 

Skills

Posted on

December 13, 2022

Submit a Comment

Your email address will not be published. Required fields are marked *