Search code examples
javascriptnode.jsshelljs

shelljs - getting process ID of process created by shelljs.exec() process


So, I'm dealing with an issue on Windows where I can't kill a process spawned using shell.exec(...).

I'm creating the process like this:

const shell = require('shelljs');

// ...

let childProcess = shell.exec('someBinary --whatever', { async: true });

And then trying to kill it like this:

childProcess.kill();

Which works on *nix but not on Windows (for some reason).

Therefore, I'm trying to get the process ID of the process ('someBinary --whatever') created by the shell.exec() process, and using that to kill the process at some later time.

It's trivial to get the PID of the shell.exec() process, but I can't work out how to get the PID of the process it spawns.

Any help would be great.


Solution

  • You can use taskkill with parameter /t to terminate all child processes along with the parent process, commonly known as a tree kill.