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?
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.
You can drop privileges after executing mongod (with process. setuid() and process. setgid() ) if you want.
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.
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.
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']);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With