【问题标题】:nodejs exec command failing with no useful error messagenodejs exec 命令失败,没有有用的错误消息
【发布时间】:2012-12-28 11:43:20
【问题描述】:

这是要执行的代码



    cp.exec("cc -Wall /tmp/test.c -o /tmp/test", function(e, stdout, stderr) {
        if (e) {
            var errorstr = "Compilation failed with the following error
"+ e.message.toString() client.send(errorstr) console.log(e, stdout, stderr) ee.prototype.removeAllListeners() } else if (stderr.length > 0) { client.send("Compilion finished with warnings\n"+ stderr + '\n') client.send('compiled') ee.prototype.emit('compiled') } else { client.send("Compilation successful") ee.prototype.emit('compiled') } })

'client' 是 socket.io 的回调参数的参数。 'ee' 是 EventEmitter 的一个实例

解决问题。在运行代码时,回调说命令不成功。 console.log(e, stdout, stderr) 是

{ [错误:命令失败:] 杀死:假,代码:假,信号:未定义}''''

/tmp/test.c 是有效的 C 代码,在检查目录 /tmp 时,我发现 test.c 是正确的,并且正在生成二进制“测试” 并在其中运行一个shell,被正确执行。所以我不明白为什么它会标记执行不成功。错误对象的信息也无济于事。希望得到一些帮助/解释

【问题讨论】:

    标签: javascript node.js exec child-process


    【解决方案1】:

    我有点担心控制台的输出。

    { [Error: Command failed: ] killed: false, code: false, signal: undefined }
    

    看起来不像是一个合适的 JSON/JavaScript 对象,尤其是 [Error: Command failed: ] 部分;至少少了一个逗号。

    建议:

    1. 从命令行运行命令并检查退出代码(使用echo $?)。如果退出代码是 != 0,那么这意味着命令“失败”(无论这意味着什么)。

    2. 当命令失败时,nodejs says it will put the exit code 变为 e.code(我在您的输出中遗漏了...)。找出这个值发生了什么。

    3. 尝试if(e !== null) 而不是if(e)不应该有所作为。

    4. 不要直接调用编译器,而是调用将 stderr/stdout 重定向到文件的 shell 脚本(或使用 cc ... |& tee /tmp/cc.log 保存副本)以确保复杂设置的任何部分都不会吞下重要信息。

    【讨论】:

    • (1) 我按照你的建议做了,退出码是0 (2) 好了,这里输出了整个error对象。所以 e.code 将是错误的(是的,this 最让我难过)(3)不。没有区别 (4) 事情是,我不需要 这个操作的标准输出,因为大多数情况下标准输出和标准错误将为空,因为这是有效的 C 代码
    • @ShrikrishnaHolla 你的节点是什么版本?
    • @xiaoyi: v0.9.6-pre(我最近从github克隆)
    • (2) 听起来您应该提交带有测试用例的错误报告。另外,既然你有源代码,试着找到实现exec() 的地方并检查它的真正作用。也许文档只是过时了,if(e.code) 现在是正确的。
    • @ShrikrishnaHolla 您是否尝试过使用稳定版本的相同代码?比如说 0.8.17?
    猜你喜欢
    • 2019-03-15
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 2022-10-25
    相关资源
    最近更新 更多