0

I am currently figuring out how I can kill a process(tree) but I can not find a working solution. My System is Windows 10. I tried many things, but not a single one works. Here is my code:

const { spawn } = require('child_process');
const kill = require('tree-kill');
const path = require('path');
////////////////////////////////////////////////////////////////
const psTree = require('ps-tree');

var killfn = function (pid, signal, callback) {
  signal = signal || 'SIGKILL';
  callback = callback || function () {};
  var killTree = true;
  if (killTree) {
    psTree(pid, function (err, children) {
      [pid]
        .concat(
          children.map(function (p) {
            return p.PID;
          })
        )
        .forEach(function (tpid) {
          try {
            process.kill(tpid, signal);
          } catch (ex) {}
        });
      callback();
    });
  } else {
    try {
      process.kill(pid, signal);
    } catch (ex) {}
    callback();
  }
};

////////////////////////////////////////////////////////////////

//I spawn the process here:
const projectPath = path.join(__dirname, '../');
const pathToPython = path.join(projectPath, '/Venv/Scripts/python');
const pythonOptions = [path.join(projectPath, '/pythonBackend/app.py')];

let ls = spawn(pathToPython, pythonOptions);


const endPython = () => {
 console.log('shutting down python Server...');
    // kill(ls.pid); //does not work.... Pyhton Server is still running
    // ls.kill('SIGINT'); //does not work..
    // process.kill(-ls.pid); //does not work; throws error: Uncaught Exception Error: kill ESRCH
    killfn(ls.pid); //even this does not work...
}

setTimeout(endPython, 5000);

I don't know what I am doing wrong. The Python app is still running and all "solutions" I found on stackoverflow or somewhere else doesn't work.

swftowu69
  • 123
  • 1
  • 9
  • Simply [`ls.kill()`](https://nodejs.org/api/child_process.html#subprocesskillsignal)? Don't go via `process` and the `pid`. Btw, did you notice the [`timeout` option to `spawn`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options) (if that's your use case)? – Bergi Apr 12 '22 at 22:09
  • Hi @Bergi , this does not work. The python process is still running. The timeout function in my code is just an example. Later the `endPython` function will get called from a user interaction. – swftowu69 Apr 13 '22 at 07:39
  • What is the Python application doing? How will it respond to the signals? Have you tried sending a different signal? – Bergi Apr 13 '22 at 07:41
  • On the python applications runs a Flask Server. Thats it. I tried different signals like `SIGINT` or `SIGhUP` but they don't work. There MUST be a way to force the killing of process. The systems creates a process tree, right? So there must be an option to kill the entire tree, but i did not found it yet. – swftowu69 Apr 13 '22 at 07:52
  • 1
    I just tried it on my MacOS machine and there the python process gets terminated with `ls.kill()` without a problem. On Windows it does not work. This is a Windows Problem... – swftowu69 Apr 13 '22 at 08:11

0 Answers0