在exec的函数回调中使用on.exit

时间:2014-07-01 15:02:10

标签: javascript node.js exec exit child-process

我有javascript代码,运行exec,类似

exec('binary with argument', function(error, stdout,stderr)
{

// Calls a callback function if error is encountered
}

我还想使用该二进制文件的退出状态发送到上面的回调函数。通常的做法是

child.on("exit", function (code) {
  console.log("exit:", code)
})

如何在exec错误处理程序中集成上面的exit实用程序,以便捕获返回错误代码并将其传递给回调函数?

1 个答案:

答案 0 :(得分:2)

  

我需要二进制文件的退出状态,以便我可以将其返回给第一个代码段中的用户定义的回调。

根据the docs,您不需要倾听exit事件来执行此操作:

  

回调获取参数(error, stdout, stderr)。成功的话   error将为null。如果出错,error将是一个实例   Errorerror.code将成为孩子的退出代码   进程,error.signal将被设置为终止的信号   过程。