Reputation: 2310
exec = require('child_process').exec;
child = exec('node child.js');
child.stdout.pipe(process.stdout);
child.kill('SIGKILL');
function wait() {
setTimeout(wait, 1000);
child.kill('SIGKILL');
}
wait();
The above code does not work. The child starts and will continue to write output indefinitely. I can not figure out how to kill this child process. I am running node v0.11.9 in Windows 7. I know that Windows does not use POSIX signals but sending it 'WM_QUIT' results in an exception. Is my best solution to setup an event protocol on stdin?
Upvotes: 28
Views: 25729
Reputation: 406
To kill it manually, use below command on mac/linux:
lsof -i tcp:<PORT>
e.g.
lsof -i tcp:3000
Then it will list processes you supposed to kill:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
Electron 34931 krishanpal 26u IPv6 0x36170510593791ba 0t0 TCP *:pxc-splr (LISTEN)
Use the PID
and hit kill port command as below:
kill -9 <PID>
e.g.
kill -9 34931
Upvotes: 0
Reputation: 51
There is no problem under macOS. Under Windows, kill only exits the process and cannot close it. Under Windows, you need to manually destroy
it.
const cp = require('child_process').spawn('your code')
// add this code
cp.stdout.destroy()
cp.stdin.destroy()
cp.stderr.destroy()
cp.kill()
Upvotes: 2
Reputation: 9630
Just an update
Now this code works (tested with Node.js 8.9.3 and Windows 10):
spawn = require('child_process').spawn;
child = spawn('node', ['child.js']);
setTimeout(function() {
child.kill();
}, 5000
);
Upvotes: 5
Reputation: 36353
I had to use the following package to kill my child process:
https://www.npmjs.com/package/tree-kill
The regular .kill command wouldn't work for me either on a raspberry pi.
Upvotes: 11
Reputation: 1
check this code it worked for me.
var killer = require('child_process'); killer=exec('taskkill /F /pid '+child.pid);
Here killer is a variable and child is your child process.
when you create a child process it has many attributes associated with it and pid is one of them.
for more details on child process attributes and function check this node.js child process.
This program is to kill child process in windows environment.
Upvotes: 0
Reputation: 684
This still doesn't work for me with the current accepted answer. A work around on windows you can use is to call upon the windows taskkill program to kill the child process for you. Not quite as nice but it works. When you spawn the child you get a ProcessID (pid) stored in the child object returned when spawning, you can use with taskkill to kill the process.
var spawn = require('child_process').spawn;
spawn("taskkill", ["/pid", child.pid, '/f', '/t']);
Upvotes: 56
Reputation: 39532
If you want to be able to kill child processes via SIGKILL
, use spawn
instead, as spawn
will create a child process (instead of a new shell like exec
:
var exec = require('child_process').spawn;
Alternatively, you could pass the timeout
parameter to exec
, which will kill the process after that many milliseconds.
child = exec('node child.js', { timeout: 1000 });
Upvotes: 7