1

I'm using childprocess.execFile to run a phantomjs script with the code below, and everything is fine when it runs successfully but if there is an error in the script the callback is never called ("child process finished" doesn't show in the logs).

How should this situation be handled? My app needs to know if the child process has failed

  childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) {
      console.log('child process finished');
      console.log(stdout);
      console.log(err);
      console.log(stderr);
      res.json({'status':'finished'});
      // handle results
    })
Omiron
  • 341
  • 3
  • 15
  • Does the child process actually exit? – robertklep May 24 '17 at 10:27
  • This happens if I try and use say a variable that doesn't exist, something that would make it crash with an unhandled exception – Omiron May 24 '17 at 10:32
  • https://stackoverflow.com/questions/34208614/how-to-catch-an-enoent-with-nodejs-child-process-spawn – kWeglinski May 24 '17 at 11:00
  • @Mort `spawn` is for long-running processes, so catching errors there is different than with `execFile`. I would expect that _if_ the process exits with an error, the `err` argument would be set. However, it seems to me that the child process isn't actually exiting. – robertklep May 24 '17 at 11:12
  • Not an exact duplicate, but the other question leads me to use spawn rather than execFile – Omiron May 24 '17 at 11:12
  • @Omiron well, `execFile` actually _uses_ `spawn` under the hood ([here](https://github.com/nodejs/node/blob/ccd3eadbd7dae3a23d43bf490fa9d3019324370e/lib/child_process.js#L190)). – robertklep May 24 '17 at 11:15
  • @Omiron try and see if `childProcess.execFile(...).on('error', ...)` works for you. – robertklep May 24 '17 at 11:47

0 Answers0