How to kill the pm2 --no-daemon process - node.js

I'm using pm2 as the process manager of Node.js.
In many cases, I think I will run it as a daemon process, but if you use it locally as debugging, I think that there are times when you use the --no-daemon option.
How do I end the process when moving pm2 with this --no-daemon option?

You can try:
pm2 kill
or find the running PM2 process with:
ps aux | grep PM2
then kill with:
kill -9 [pid]
The -9 switch sends the KILL signal to the process as opposed to the default interrupt (INT or SIGINT) signal and is equivalent to -KILL or -SIGKILL. Interrupt is a less invasive way and you could try that first to let the process gracefully exit, however, if it doesn't respond to that, the kill signal should result in an immediate termination (unless the process is zombie).

You can view all processes which are registered with pm2 using
pm2 list
Assume the process you want to stop is named as processA using the below command will stop the processA:
pm2 stop processA
In case you want to delete the process than use the below command:
pm2 delete processA
In case you don't want to kill a particular process but pm2 itself using the command below:
pm2 kill

The right answer is pm2 kill
$pm2 kill
[PM2] [v] Modules Stopped
[PM2] Applying action deleteProcessId on app [all](ids: 0)
[PM2] hello ✓
[PM2] [v] All Applications Stopped
[PM2] [v] PM2 Daemon Stopped

Other solution will be to run pm2 delete all or pm2 stop all. Which will not kill pm2 process itself, but will cleanup internal pm2's process list.

First of all list all processes:
pm2 list
let suppose if your process is dev
pm2 stop dev
Now, delete the process
pm2 delete dev
after that process state became daemon.
If you want to kill that daemon process then run command
pm2 kill

sudo pkill -f pm2
This should kill all processes of pm2 in linux

One thing to add to the accepted answers. These commands only work for the current user. I had the same problem with a digitalocean droplet. I had logged in using "ubuntu" username, but I saw that the God Daemon is pointing to /home/nodejs/.pm2.
If this is the case, you need to switch to that user:
sudo su nodejs
And then run the pm2 kill commands from there.

If it's running in the foreground you should be able to kill it with ctl + c, same as you would kill node server.js.

Related

pm2 hangs and unable to restart process due to high numbers of request

For the purpose of load testing i created an API which request around 2000 bookings at a time and after hitting this API, server(AWS EC2 instance) reaches to the more than 20% of CPU consumption and consume almost all the memory(900/991M) of system. After that i was trying to restart node app with pm2 restart but it's not working and even "pm2 ls" and "pm2 log" commands are also not working. What can i do now to run my application again ?
List of command which i already run on server.
pm2 restart pid && pm2 log pid
pm2 ls
pm2 log pid
Try to kill the current process of pm2:
ps -ef | grep pm2
kill -9 <PID_OF_PM2>
And then restart pm2 again.
Try to Upgrade to the latest pm2 version if the above not working after killing the pm2 process, using this command:
npm install pm2#latest -g
PM2 hangs when your project contains millions of files and it is watching all those files. Make sure to exclude the non-essential files from watch using ignore_watch.

Does supervisord kill running processes on with the restart command?

We have supervisord running in production and I'd like to know if after running $ supervisorctl restart group-name:* if running processes are killed immediately or if supervisor lets running processes finish.
Tried my best to find that out in the docs and the source code.
As far as I know, supervisorctl will kill process, because usually under supervisor work worker script which never stops, hence it is no way to supervisorctl understand when script is ready to be stopped.

Why pm2 deleting process automatically?

I am running 3 process in pm2. But after 2-3 month my process are getting deleted from pm2 list. Its like we just installed pm2.
I want to know why this is happening and how to rectify it.
Are you sure that the PM2 daemon is still alive when you're doing the pm2 list ? If you run out of memory, chance are that your OS kill the pm2 daemon and by the way killing your application since they are process child of pm2 daemon.

Monit check processes for multiple pidfiles

I have a Nodejs Web app with multiple node process, which I start using pm2 start app.js multiple times.
To monitor these processes usinf monit, I created a init script using pm2's : pm2 startup ubuntu command. Then, in the monit config file for my App, I use this init script as start and stop program commands for monit. Then I use something like check process pm2_1 with pidfile /path/to/node-pidfile, for both the processes in the monit config file.
I would like monit to check the pidfiles of both these processes and when either or both processes are down, restart both the processes. So, here's what my my-webapp.monitrc looks like :
check process pm2_1
with pidfile /root/.pm2/pids/proc1-0.pid
start program = "/etc/init.d/pm2-init.sh start"
stop program = "/etc/init.d/pm2-init.sh stop"
check process pm2_2
with pidfile /root/.pm2/pids/proc2-1.pid
start program = "/etc/init.d/pm2-init.sh start"
stop program = "/etc/init.d/pm2-init.sh stop"
The problem is, if either of the processes is down,it works. But if both processes are down, monit executes the start command twice.
Is there a way to have a "OR" condition for check process to monitor multiple different pidfiles and execute the same start and stop commands only once ?

How to reset the id of pm2?

I using pm2. After I started my app several times, the id increased. I deleted all in pm2, and started again, but the id didn't count from 0 any more. How can I reset it?
The solution is to restart pm2, by typing pm2 kill as said by ItalyPaleAle in the comments.
Per pm2's GitHub:
$ pm2 reset <process> # Reset meta data (restarted time...)
Reference: GitHub issue#1456
Looking at the help menu (pm2 --help) it seems like pm2 reset would be the way to go. But that command resets metadata (restarted time, etc) and does not reset IDs. One solution is to use pm2 kill to kill the pm2 daemon and restart it again (thanks, #Yao Zhao). But doing so will stop all current processes. A better approach would be to save the list of current processes, restart pm2, and then restore those processes using the following set of commands.
$ pm2 dump
$ pm2 kill
$ pm2 resurrect

Resources