Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

226
Visualizações
How to kill a python process that is previously spawned by 'spawn' in nodeJS?

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.

about 4 years ago · Santiago Gelvez
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda