Tuesday, 25 April 2017

How can I kill a child process run by exec on Windows?

I must use the method exec on Nodejs because I need to do some stdout redirections and run more than one command at the same time. So I run the child process like this:

const child_process = require('child_process')
var command = 'activate python_environment & bokeh serve project_folder/ 2>&1';
shell = child_process.exec(command); 

I need ps-tree to kill the children as I do in the belower code. It works well on Ubuntu. But the problem is that I get a orphan (zombie) process on Windows if I do not kill the child proceses.

const psTree = require('ps-tree')
if(shell != null){
    psTree(shell.pid, function (err, children) {  // check if it works always
        children.map(function (p) {
            process.kill(p.PID);                    
        });
    });
}

The ps-tree module uses the command ps on linux and the command wmic on Windows. So it is crossplatform. But it does not work in my case. Is there a better way to do this?

I was exploring in the ps-tree source code but I found nothing.

I use process.kill because I am using electron as well.

I am afraid I should try to do it manually getting all the processes list with wmic as ps-tree is trying to do.



via ChesuCR

No comments:

Post a Comment