问题
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
回答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
回答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.
回答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/
来源:https://stackoverflow.com/questions/51205048/how-to-restart-nodejs-with-cpanel