Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

332
Vistas
How to REALLY kill a child_process nodejs

I'm using mocha with Nodejs to test my restApi. When I run mocha, I tell my test to create a child_process and run the API so I can make requests to it.

The problem is whenever the test exits (finishing or crashing), it seems that the API keeps running on background. I've seen some answers here that instructs to manually kill the child process whenever the main process exits. So I did it like this:

export function startProcess(done) {
    const child = spawn('babel-node', ["app.js"]);

    child.stdout.on("data", function(data) {
        data = data.toString();
        // console.log(data.toString());

        if(data.indexOf("Server online") > -1) done();
    });

    child.stderr.on('data', function(err) {
        console.log("ERROR: ", err.toString());
    });

    child.on('exit', function(code) {
        console.log("PROPERLY EXITING");
        console.log("Child process exited with code", code);
    });

    process.on('exit', function(code) {
        console.log("Killing child process");
        child.kill();
        console.log("Main process exited with code", code);
    });
}

When the main process exits it does log "Killing child process", meaning that child.kill() was indeed called. But if I try to run my test again, when the spawn command gets called, the API throws an error

Error: listen EADDRINUSE :::3300

, meaning that the API is still running and that port address is taken.

So I have to run sudo pkill node to really kill all node process and then npm test works again.

Am I missing something? Is this really the way to achieve what I'm expecting?

I thought about using child_process.exec to run sudo pkill node on my process.on('exit') listener, but that doesnt seem like a smart thing to do.

This is happening both in Mac and Ubuntu.

Any suggestions? Thanks

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

"exit" is an event that gets triggered when node finishes it's event loop internally, it's not triggered when you terminate the process externally.

What you're looking for is executing something on a SIGINT.

Have a look at http://nodejs.org/api/process.html#process_signal_events

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda