skip to Main Content

I need to know what to use from root side of cPanel based server to restart NodeJS app, for example, if process terminated now for some reasons NodeJS app will not start until I manually start it, same if server restart I need manually to restart it.

Also, this is case for several accounts on server, command should allow more apps to be restarted/started.

Any help would be great

3

Answers


  1. Here is an automated way to do the it:
    – Find if node server [eq. server.js] is running or not.
    – If server is not running, restart by “nodemon server.js”.
    – Else if server is running, do nothing.

    You can code this in bash script [sample code below] and set up a CRON job in your cpanel to run it after a particular time.

    #!/bin/bash
    
    NAME="server.js" # nodejs script's name here
    RUN=`pgrep -f $NAME`
    
    if [ "$RUN" == "" ]; then
      nodemon server.js
    else
      echo "Script is running"
    fi
    
    Login or Signup to reply.
  2. I just used a crone job to run the following command once every 5mins

    ~/bin/node ~/backends/api/app.js
    

    I was having problems with nodemon, it was saying it’s not a command blah blah so I thought of just directing straight to node and directly to my app.

    This is working for my use case coz the app bails if the addr is being used. So if it crashed then it will restart it since it won’t occupy the addr.

    Login or Signup to reply.
  3. I’d recommend running your node app using PM2.

    npm install pm2 -g
    pm2 start app.js
    pm2 restart app

    If you are using node or nodemon on Linux machine I’d recommend using PM2 to manage the service. It is a lot more stable that nodemon and offers other production level features like console.log to console.error file and

    https://pm2.io/doc/en/runtime/features/commands-cheatsheet/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search