PM2 stop in cluster mode - node.js

By mistake , I run the following command on the server
pm2start app.js -i 3500
Now on server multiple instances are creating in cluster mode. Can anyone suggest how can i stop this ? And how can i delete already instances ?
Thanks

Pm2 generally gives a name to process group. You can check that with pm2 list and then run
pm2 delete processname
Or run pm2 delete all to delete all processes

Related

Should "pm2 save" need to be execute again after some script updates?

currently i'm using pm2 to manage my process, and i'm still new to this. I've search some docs but still not sure.
About autostartup, what is the propper way to update my nodeJS script? right now, i just execute pm2 restart all after i update some of my script. But since i want to use pm2 startup. Do i need to execute pm2 save also ?
Also on what occasion we need to update PM2 Startup Script? pm2 unstartup then pm2 startup ?
No you don't need to execute pm2 save if your script content change.
pm2 save is only for saving PM2 process configuration. So if the startup script change of location or name, you need to execute pm2 save.
Once executed pm2 save permit you restart all process after a crash or reboot with the command ``pm2 resurrect```
To restart a process use pm2 restart <id> <here id is the id of the process. You should avoid use pm2 restart allfor restarting only one process.
If you use pm2 startup, you need to do a pm2 save each time a process is added or process ecosystem change. pm2 startup just manage for you the automatic starting of PM2 when server reboot.

How to migrate pm2 processes from one server to another?

I am using pm2 for managing node processes on one of the servers.
The package is here: https://pm2.keymetrics.io/
It is open source and available both on npmjs and GitHub.
I can easily install it every time using: npm i pm2 -g
I love pm2, and not just node processes, I write bash scripts and run them as cron under pm2 and I can easily check the logs.
Some commands:
pm2 --name "process-name" start "bash script.sh"
pm2 --name "node-process" start "node main.js"
pm2 logs node-process
pm2 stop node-process
pm2 restart node-process
There are 2 more commands which are very useful to start pm2 on startup with all the processes automatically.
pm2 startup Will generate startup script.
pm2 save Will update start script with current processes.
Everything is good. But, today I got into a problem.
I am running all pm2 node processes from a folder /mnt/node.
What I want is that I have synced that /mnt/node folder to another server and I am trying to find a way to move all pm2 processes automatically to another server without writing each process once again.
May be someone can help.
You can do this.
On the source server:
pm2 save
copy file saved on ~/.pm2/dump.pm2 to destination server, then:
pm2 resurrect
Haven't try this between two differents server yet but i think it will be ok.

Getting error when trying to launch node.js app with PM2 Cluster

in an Ubuntu Server, I am unable to run the node.js app in cluster mode using PM2.
The command I use is :
PM2 start server.js --name Server -i max
When I list the PM2 processes, I can see the Server has Error status.
I have tried looking into the log file generated by PM2 but it's empty.
I am however able to run the same server.js without the cluster mode using :
PM2 start server.js --name Server
doing PM2 Kill and starting all the services again was the solution to above issue.
You could also have used pm2 restart Server to restart it
If you use pm2 kill you will just kill all processes, to clean up afterwards i would recommend to use pm2 flush so all logfiles will be reset
I have gone through this same kind of situations but in my case pm2 is showing error status cause of error in my code.
use the below command
pm2 logs
pm2 logs command helped me by showing some hints to check where exactly the error is occured.
if everything works fine then pm2 list will show you the status online.
you can check the ports running by pm2(not only pm2 but all the process) using below command
sudo netstat -tulpn

How to restart a PM2 process with process name instead of process id?

When we are running multiple node services in a single machine, its becoming hard to restart one service with id as id is a sequence number allocated in order by PM2. So instead using the id is it possible to restart the process using process name?
I start my services with the service name using following command:
pm2 start index.js --name my-service-name
I want to restart them using the name 'my-service-name' so that I can automate it using a script.
From the documentation:
pm2 start web.js --name "web-interface"
pm2 restart web-interface

PM2 not making clusters

I just installed pm2, I already has done clustering in my app.js using node cluster module.
When I use pm2 start app.js -i 4 to start my app.js it only show me one online instance. It should show me 4 instance Please tell me what is the problem
Screenshot attached
Run this command...
it should work.
pm2 stop all
pm2 delete all
pm2 start app.js -i 2
where i is the number of instance you want to start.
Always use pm2 delete all to unregister the CPU, since if you stop it, it still reserve the CPU

Resources