Issue
I don't have any errors in my node app, but it stops after some requests. Though i have enabled the logs and checked, its showing on different requests.
Is it possible to start the node app whenever it is stopped ?
Currently i am using this to start the node app
To start the app
nohup npm start &
And to find the process id
ps aux | grep node
And to kill certain process
kill -9 PID
I don't want to leave the app stopped. It should be ever running. So, Is it possible to start the app whenever it gets stopped.
Thanks
Solution
You can use PM2 to monitor and restart your app for you. By default if you run your app with PM2 it will auto-restart it if it crashes.
npm install pm2@latest -g
pm2 start index.js
PM2 can also be used to run multiple instances and has commands to stop/restart individual ones.
You can get the process PID using pm2 list
as well.
More robust use case:
Start up your app by having PM2 run 'npm start' and also give it a name to refer to it later (it will auto restart if it exits/crashes)
pm2 start npm --name="mySuperApp" -- start
Later you make some changes and want to restart it:
pm2 restart mySuperApp
Eventually you want to stop the app:
pm2 stop mySuperApp
Answered By - CodingWithSpike Answer Checked By - Pedro (WPSolving Volunteer)