How to restart NodeJS with cPanel
Asked Answered
G

3

2

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

Giffer answered 6/7, 2018 at 7:23 Comment(0)
D
2

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
Drain answered 6/7, 2018 at 7:33 Comment(1)
I use like this, but it show error: s33.postimg.cc/vfa7cpmnz/20180515_213815.jpg and if I execute over SSH: pm2 start mooServer.js it work fine, but over cron it is missing pid as you can see in screenshot. #!/bin/bash NAME="mooServer.js" # nodejs script's name here RUN=pgrep -f $NAME if [ "$RUN" == "" ]; then /home/preppers/bin/node /home/preppers/bin/pm2 start /home/preppers/public_html/lib/MooNodeJsServer/mooServer.js else echo "Script is running" fiGiffer
S
1

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/

Seamy answered 21/5, 2019 at 15:34 Comment(0)
R
0

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.

Rarefaction answered 26/3, 2019 at 18:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.