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
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.
I just used a crone job to run the following command once every 5mins
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.
I’d recommend running your
node
app usingPM2
.npm install pm2 -g
pm2 start app.js
pm2 restart app
If you are using
node
ornodemon
on Linux machine I’d recommend usingPM2
to manage the service. It is a lot more stable thatnodemon
and offers other production level features likeconsole.log
toconsole.error
file andhttps://pm2.io/doc/en/runtime/features/commands-cheatsheet/