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

93
Vistas
I want to get all the returns in real time from a script that sends several lines of information in a row

That is the problem I need to get all the outputs of a script that I run from node js and get all of them, now I get them but only when the script process is completely finished and I want to get them every time it sends them. This is the code:

    exec(line, (error, stdout, stderr) => {
        if (error) {
            out = "!!!!!!! ERROR EN ComandLine (" + Today() + ")-------!! : \n " + error.message;
            console.log(_out);
            WriteLog(_out);
            socket.send("ERROR");
            return;
        }
        //Standar error
        if (stderr) {
            out = "!!!!!!! STDERROR EN ComandLine (" + Today() + ") : \n " + stderr.message
            console.log(_out);
            WriteLog(_out);
            socket.send("ERROR");
            return;
        }
        //resultado final

        if (stdout.toString().includes("Se fini")) {
            console.log("Server end task");
            _generating = false;
        }
        _out = "------- STD OUT ------- (" + Today() + ") : \n " + stdout.message;
        console.log(_out);
        WriteLog(_out);
    });
}

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I suppose that you are using child_process.exec. For your use case child_process.spawn would be the right choice I think since it is emitting events you can listen to.

See the nodejs doc https://nodejs.org/api/child_process.html#child_processspawncommand-args-options

Something like this should roughly implement the behavior you are looking for.

const { spawn } = require('child_process');
const cmd = spawn(line);

cmd.stdout.on('data', (data) => {
  _out = "------- STD OUT ------- (" + Today() + ") : \n " + data;
  console.log(_out);
  WriteLog(_out);
});

cmd.stderr.on('data', (data) => {
  _out = "!!!!!!! STDERROR EN ComandLine (" + Today() + ") : \n " + data
  console.log(_out);
  WriteLog(_out);
  socket.send("ERROR");
  return;
});

cmd.on('close', (code) => {
  console.log("Server end task");
  _generating = false;
});

about 4 years ago · Juan Pablo Isaza 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