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

366
Vistas
How to ignore errors generated by child_process.exec?

I am executing shell script commands from my nodejs script. One of these commands is "npm install" followed by a command to run the index file of a nodejs file.

The npm install command is returning an error generated by node-gyp. In general, this error does not affect my service. However, child_process.exec is catching it and stopping the script. My questions is, how do I trigger the exec command and ignore the error returned?

Below is a fraction of the code snippet

const exec = require('child_process').exec;
exec("npm install", {
            cwd: serviceDirectory + gitRepo
        },
        (error1, stdout, stderr) => {
            if(error1){
                //this error is for testing purposes
                util.log(error1);
            }
            //run the service
            exec("node index.js",{
                cwd: serviceDirectory + gitRepo + "/"
            }, cb);

        });

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

0

You can use try-catch to handle the errors with catch, example:

const { promisify } = require('util');
const exec = promisify(require('child_process').exec);

export default async function () {
  const dataFormat = {
    stdout: '',
    stderr: '',
  };
  let cpu = dataFormat;
  let diskUsed = dataFormat;

  try {
    cpu = await exec('top -bn1 | grep "Cpu(s)" | sed "s/.*, *\\([0-9.]*\\)%* id.*/\\1/"');
  } catch (error) {
    cpu.stderr = error.stderr;
  }
  try {
    diskUsed = await exec("df -h | awk 'NR==2{printf $3}'");
  } catch (error) {
    diskUsed.stderr = error.stderr;
  }

  const payload = {
    cpu,
    diskUsed,
  };
 return payload
}
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