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

标签 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 错误处理程序中,以便捕获返回错误代码并将其传递给回调函数?

最佳答案

I need the exit status of the binary so i can return it back to user defined callback in the first snippet.

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

The callback gets the arguments (error, stdout, stderr). On success, error will be null. On error, error will be an instance of Error and error.code will be the exit code of the child process, and error.signal will be set to the signal that terminated the process.

关于javascript - 在 exec 的函数回调中使用 on.exit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24513529/

相关文章:

javascript - visibilitychange 是如何工作的?

javascript - Javascript 对象何时从内存中被垃圾回收?

javascript - Passport facebook 策略 throw 数据必须是使用 Node.js 的字符串

node.js - 在服务器和任意 NodeJS 进程之间创建 Web 套接字流

c - 参数大小未知的 execl 与 execv

powershell - 使用 WshShell.Exec() 方法调用 Powershell 会挂起脚本

C Linux - 如何从另一个程序执行程序

javascript - 标签选择器控件

javascript - 表单无法从移动设备提交

node.js - Express.js - 如何为所有响应设置标题