Monit check processes for multiple pidfiles - node.js

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 ?

Related

Why my node server application stops/ goes to suspended mode when i kill its child process?

I was building an application with node.js as back end, which runs shell scripts inside it.
I have added a feature to stop the execution of script .
For running script I use the exec process in node.
But when I kill the child process created in exec, the application also stops.

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.

How to kill the pm2 --no-daemon process

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.

How to check a process is running in monit

check process myproc with pidfile /opt/management/myproc.pid
start program = "/etc/init.d/myproc start"
stop program = "/etc/init.d/myproc stop"
if 3 restarts within 3 cycles then timeout
mode active
group local
Above is monit file for myproc. When I clone my vm, sometimes the myproc.pid file is exist but actual process is not running in the cloned vm. In this case, monit still thinks myproc is running and not attempting to start my process in newly cloned vm.
Any suggestions to solve this issue?

Linux command to restart application

For example - i have process id which i want to restart. What command i should use to restart this process application ? I didn't find something about it(
Thanks!
You can find very similar question at Restart process script linux.
Linux doesn't have general command for restart, normally you should kill your process and start it over. However, if your process has been started as a service, i.e. it's contained in /etc/init.d/ directory, then you can do the following:
/etc/init.d/SERVICE_NAME restart
or
service SERVICE_NAME restart

Resources