Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

360
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!