Node child process doesn't release port, even after it's killed by parent - node.js

I have a parent node process and a child node process. The child binds a web server to port 5000. The parent sometimes kills and restarts the child (usually because a file has changed).
The problem is: when the second child process starts, I get Error: listen EADDRINUSE :::5000.
The weird thing is, I kill the child process with child.kill(), then I wait for it to fire a close event, and then I wait 5 seconds with setTimeout, before trying to start a new child process...yet it always complains that the port is occupied. But if I manually kill the parent process (ctrl+C in my terminal) and run it again, I don't get the error. So killing the parent seems to be the only way to successfully release the port.
Can anyone think of a reason why this would happen? Why does the port remain occupied after the process that bound it is killed, until its parent process stops?

Actually it block the process so you need to kill the process by following command just type
ps -ax
You will get list of processes from where you need to check the process id which is been blocked then kill the process by this command
kill -9 processId

Kill all your running port by below command.
pkill node

Related

Linux process automatically starts again with new new PID after killing

I wanna stop Node server on linux but when I try to stop it, It automatically starts with new PID how can I stop this completely. Here you can see I tried to stop Nginx and Node.
How can I check if it is a linux zombie process? If yes then How can I kill it? otherwise
I have tried tried these commands.
kill pid
Kill -9 pid
killall node <<command not working
This was spawn by it's parent process, I search out parent process after killing that this stop as well.

Ubuntu: Is there any difference of killing a child process when the child process runs in the foreground or background?

Open a terminal
Open the second terminal
Excute command "$ xlogo" in the sceond terminal
Find out the parent process of xlogo in the first terminal
(I find out that "bash" is the parent process)
Terminate the parent process of xlogo in the first terminal
(Command:$ kill -9 PID of parent process)
At this moment, "xlogo" and the second terminal are gone.
Open the third terminal.
Excute the command "$ xlogo &" in the third terminal (Let xlogo running in the background)
Teminate the parent process of xlogo in the first
terminal
(Command:$ kill -9 PID of parent process)
At this moment, "xlogo" still alive, but the third terminal is gone.
By excuting the command "$ pstree", I find that "xlogo" belongs to "systemd".
Questions:
a) Why "xlogo" was killed with its parent process when "xlogo" ran in the foreground?
b) Why "xlogo" still alive and didn't die with its parent process when "xlogo" ran in the background?
I am searching for a long time on the net. But get nothing.
Could anyone try to give some ideas on how to explain this?
thx

Send KillAll to multiple node process and wait response

I'm creating a script that makes some pull from a git repository.
but first i want to send a SIGUSR1 to multiple nodes that i have running in the machine, and once all the procesess stop i will proceed to git pull
so basicly i will run
killall -s SIGUSR1 node
because my node is doing an infinite loop, i want to intercept this signal and kill in a portion of the loop where i know it won't affect any data.
So when all the nodes are stopped i will continue
but how do i wait for all the node process exit cleanly before doing the git pull and starting again the node loop
From man killall:
-w, --wait
Wait for all killed processes to die. killall checks once per second if any of the
killed processes still exist and only returns if none are left. Note that killall may
wait forever if the signal was ignored, had no effect, or if the process stays in zombie
state.

How to make killall close the terminal that the process is in?

So how can I close the terminal where the process is in with killall.
I have tried this:
In 1st terminal:
killall node
In 2nd terminal:
Ready
Terminated
But I want only the 2nd terminal to close after the node is killed.
You can use the -t option:
killall -t $(tty)
will call all processes started from the terminal session (even with nohup), including the shell. So, your terminal will get closed.
You need to also kill the process which runs the terminal, which is usually the parent process of the node process.
The question How do I get the parent process ID of a given child process? is a good place to start. You can find the PIDs of the node processes via How to find the Process ID of a running terminal program.

How to kill Node Express child process?

Inside one Node/Express server, I started another Node/Express server as child process:
let appifi = child.spawn('node', [babel_path, www_path], {
cwd: appifi_path,
env: appifi_env,
})
This worked fine and appifi got a pid, say 2376.
When trying to stop the child process, appifi.kill() will kill the process with pid 2376, but there is a respawned server process running, usually with a pid equals to it's parent's pid plus 5 (I don't know if this is a strict rule).
My question is, how to kill them both in parent server? is it safe to process.kill(appifi.pid + 5)? or there are any better ways?
You can kill both (actually, ALL) node servers by killall -9 node

Resources