Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I not kill my child process in nodejs on windows?

Tags:

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?

like image 392
Nick Sotiros Avatar asked May 16 '14 23:05

Nick Sotiros


People also ask

How do I kill a child process in Windows?

The solution is to use "job objects" http://msdn.microsoft.com/en-us/library/ms682409(VS.85).aspx. The idea is to create a "job object" for your main application, and register your child processes with the job object. If the main process dies, the OS will take care of terminating the child processes.

How do I close a node js child process?

You can drop privileges after executing mongod (with process. setuid() and process. setgid() ) if you want.

How do you kill the process of a child?

For killing a child process after a given timeout, we can use the timeout command. It runs the command passed to it and kills it with the SIGTERM signal after the given timeout. In case we want to send a different signal like SIGINT to the process, we can use the –signal flag.

Do you kill a process in node JS?

kill() Method. The process. kill( pid[,signal] ) is an inbuilt method of node. js which sends a signal to the process, pid (which is the process id) and signal is in the string format that is the signal to send.


1 Answers

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']); 
like image 58
Leigh Nathan Beattie Avatar answered Oct 12 '22 13:10

Leigh Nathan Beattie