How to kill actually running node application from itself - node.js

I have a node application that sometimes breaks then restart, but after this restart node process of this application Is still running, it can kill my small VPS server when the app will restart a lot of times and I will not kill these process manually.
How can I kill these processes from the node application?
I have a function that restarts the application, so I want to put some code inside this function that will kill this node process before or after starting a new one.
How the node can kill the process by itself?
I mean about something like kill PID or process.kill(PID) - but how can I get the PID of the actual running application?

Sounds like you are trying to kill the Node instance.
process.exit();
In which case, just add process.exit([code]) to the end of your 'start a new server' function.
In a nutshell, process.exit() kills the node app.
For more information:
https://nodejs.org/api/process.html#process_process_exit_code
To get the PID
If you still need the PID; you should be able to get the PID with process.pid; which returns the PID as a Number.

Related

How to restart node app (typescript) when using supervisor?

I have a NodeJS server written in Typescript, and I'm using supervisor to run this app in the background in production. However, when I type supervisorctl stop myapp, the process stops, but the node process is still running in the background and the site is live. How do I stop the given nodejs process with supervisor?
If supervisorctl stop doesn't stop your node process, you can actually set the stopsignal to one of following values: QUIT, KILL
In the config file '/etc/supervisor/conf.d/*.conf' add the following:
stopsignal=KILL
For more details check this.

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.

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 ?

Reload node server on command

Is there a way to trigger the node server to reload on a command
Something like;
if(checkFirstInCmd("RESTART",cmd){
RESTART.NODE.SERVER
}
There is no built-in way to restart a node server. For example: if a node server crashes it will stop the node process.
One solution that came to my mind was something like a shell script that you could spawn with a child process. The script could kill your server and maybe wait some seconds till it's killed and than restart it again.

Resources