I have tried so many ways to keeping node server alive on Linux environment but nothing has worked. Sometime the server runs only 4-5 hours and sometime it runs 10-12 hours and after that server goes shut down automatically. I have tried forever start, pm2, nodemon but nothing has worked. I have also tried shell script with forever start for running it but that also not worked.
-
Have you checked this out? https://stackoverflow.com/questions/29536649/node-server-crashes-after-few-hours – Shikhar Tandon Oct 04 '19 at 06:00
-
There would be a reason it stops, look out for it then fix it, no process manager will work if your script is broken. – Lawrence Cherone Oct 04 '19 at 06:07
3 Answers
Applications that are running under PM2 will be restarted automatically if the application crashes or is killed, but an additional step needs to be taken to get the application to launch on system startup (boot or reboot). Luckily, PM2 provides an easy way to do this, the startup subcommand.
The startup subcommand generates and configures a startup script to launch PM2 and its managed processes on server boots:
$ pm2 startup systemd
Run the command that was generated (similar to the highlighted output above, but with your username instead of sammy) to set PM2 up to start on boot (use the command from your own output):
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u sammy --hp /home/sammy
check here for details https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04
You can increase the size of memory restar- check this: pm2 process crashed on server. it gives an error

- 409
- 6
- 15
Try using process manager for making the application run all the time. Here is the link for Pm2 . It will restart you application once it crashes also automatically

- 703
- 11
- 28
Use a NPM package called nodemon
npm install -g nodemon
nodemon index.js
If the application fails or crashes for any reason it will restart
Read more at https://www.npmjs.com/package/nodemon

- 87
- 2
- 9