I am looking for a way to kill Node.js processes using their pid. I've been searching Google, StackOverflow, and the Node.js documentation for hours. It seems like something that should exist. I can only find how to do it based on my variable newProc
below, but I cannot do newProc.kill()
because I want to kill the child process from outside of the function scope. Additionally, I need to store something in MongoDB for record-keeping purposes, so I thought pid would be more appropriate.
var pid = newJob();
kill(pid); //<--anything like this?
var newJob = function () {
var exec = require('child_process').exec,
var newProc = exec(some long running job, function (error, stdout, stderr) {
console.log(stdout: + stdout);
console.log(stderr: + stderr);
if (error) console.log('exec error: ' + error);
});
return newProc.pid;
}
EDIT: Can I use process.kill(pid)
on a child process? The docs state that it is a global object that can be accessed from anywhere.
Finally, as a slight tangent, is there a way to ensure that the pid's will always be unique? I don't want to unintentionally kill a process, of course.