pm2 restarts again and again - node.js

I have my NodeJs server hosted on DigitalOcean.The server worked well for few days after which it stopped due to pm2 starting again and again.
This is a screenshot of my logs after starting it with pm2 start app.js
However, if I simply start my server using node app.js it works fine.
My code for listening on server is
I have not found any suitable answer for this problem.If more description to this problem is required then let me know.

This mainly happens when your server does not kill previously running processes.These processes keep on running and they consume whole memory causing memory leak.
Process killing can be done using following code
process.on('SIGTERM', function handleSigterm() {
server.close();});
this will kill all the previous running process

Related

How to stop expressjs server

I have deployed my express and nextjs based app on EC2, I am trying to stop server so I kill process few second later I notice it automatically starts Nginx and node with diffrent porcess ID, I haven't added forever or nodemon etc.. it's just simple express server right now. How can I stop that.
I have tried to kill process but still my server.js is running
here my full package.json
This was automatically spawn by it's parent process, I search out parent process after killing that this stop as well.

How does PM2 manage to run multiple processes listening on the same port?

I wonder how pm2 allows my express app code to run unmodified in parallel processes. When I try to manually start another instance of my express app listening on e.g. port 3000, I always get error that port is already in use.
Does pm2 MODIFY the js code which is run to insert some tweaks in the middle, or does it emulate some sort of VM and then exposes just the one port of pm2 process to the world? What goes on behind the scene?
PM2 won't allow you to start the same script on the port again. If you use the pm2 start app.js -f option to force the script to start again, pm2 will repeatedly try starting up the script but will fail every time since that port is already in use. pm2 ls might show that there are two processes running, but in reality the second one is failing continuously and you can confirm the same by running the pm2 logs command.
If you're talking about the cluster mode of pm2, in that case pm2 just load balances the requests between multiple child processes using NodeJS's cluster module internally.

Can't stop node.js app started with pm2 process manager

I want to rebuild my node+vue.js app to update the changes I made. The app was started with pm2, but the problem is that I can't see anything when I do pm2 ls. The list is empty. On the other hand, the node app is running. I tried to kill it with pid, but it is restarting immediately.
Any suggestions?

Swagger managed nodejs server crashing when using PM2

I created my node js server using swagger via:
swagger project create <project-name>
But when I'm trying to launch the server using pm2 as follows:
pm2 start app.js
it's crashing because of too many unstable restarts. Below is the error message:
Script /path/to/app.js had too many unstable restarts (16). Stopped.
"errored"
Its a known issue and it can be fixed if we change the FD0 pipe in ForkMode from "ipc" to "ignore".
For more details please refer to this issue thread on github: PM2 crashes due to many unstable restarts

Pm2 process stops running

I have a node chat application that needs to keep running on my server (ubuntu with nginx). The problem is that the application stops after a few hours or days.
When I check on the server I see that my pm2 list is empty.
The code I use to start my app:
pm2 start notification_server/index.js
It somehow looks as if pm2 is reset after a while. I also tried using forever, but then I run into the same problem. Is there some way to prevent the pm2 list from getting empty?
This is most likely an indication that your server is rebooting. When your server reboots, PM2 shuts down and deletes all Node instances from its "status" list.
You can perform the following steps to make PM2 relaunch your Node programs start back up on reboot:
Run pm2 startup and follow the directions (you will have to perform a sudo command; PM will tell you exactly what to do).
Through pm2 start, get your Node processes up and running just like you like them.
Run pm2 save to register the current state of things as what you want to see on system startup.
Source: http://pm2.keymetrics.io/docs/usage/startup/
Did you try checking logs $ pm2 logs for you application?
Most likely it will tell you why your application was terminated or maybe it just exited as it supposed to. You could find something like that there:
PM2 | App [app] with id [0] and pid [11982], exited with code [1] via signal [SIGINT]
This can tell you what happened. Without more details, it's hard to give you a better answer.

Resources