javascript nodejs exec 命令失败,没有有用的错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14319724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
nodejs exec command failing with no useful error message
提问by Shrikrishna Holla
This is the code to execute
这是要执行的代码
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' is the argument of socket.io's callback argument. 'ee' is an instance of EventEmitter
'client' 是 socket.io 的回调参数的参数。'ee' 是 EventEmitter 的一个实例
Coming to the problem. On running the code, the callback says that the command was unsuccessful. console.log(e, stdout, stderr) is
问题来了。在运行代码时,回调表示命令不成功。console.log(e, stdout, stderr) 是
{ [Error: Command failed: ] killed: false, code: false, signal: undefined } '' ''
/tmp/test.c is a valid C code and on checking the directory /tmp , I find that test.c is proper and the binary 'test' isbeing generated and on running in a shell, is properly executed. So I dont understand why it is flagging unsuccessful execution. The error object's information is unhelpful too. Would appreciate some help/explanation
/tmp/test.c是一个有效的C代码以及检查目录/ tmp,我发现test.c的是适当的和二进制“测试”被生成并在上壳运行,则适当地执行。所以我不明白为什么它会标记执行失败。错误对象的信息也无济于事。希望得到一些帮助/解释
采纳答案by Aaron Digulla
I'm a bit worried about the output in the console.
我有点担心控制台中的输出。
{ [Error: Command failed: ] killed: false, code: false, signal: undefined }
doesn't look like a proper JSON/JavaScript object, especially the [Error: Command failed: ]
part; there is at least a comma missing.
看起来不像一个合适的 JSON/JavaScript 对象,尤其是[Error: Command failed: ]
部分;至少缺少一个逗号。
Suggestions:
建议:
Run the command from the command line and check the exit code (use
echo $?
). If the exit code is != 0, then this means the command "failed" (whatever that might mean).When the command fails, nodejs says it will put the exit codeinto
e.code
(which I'm missing in your output...). Find out what happened to this value.Try
if(e !== null)
instead ofif(e)
. Shouldn'tmake a difference, though.Instead of calling the compiler directly, call a shell script which redirects stderr/stdout to a file (or save a copy using
cc ... |& tee /tmp/cc.log
) to make sure no part of the complex setup swallows important information.
从命令行运行命令并检查退出代码(使用
echo $?
)。如果退出代码是 != 0,那么这意味着命令“失败”(无论这意味着什么)。当命令失败时,nodejs 说它将把退出代码放入
e.code
(我在你的输出中缺少它......)。找出这个值发生了什么。尝试
if(e !== null)
代替if(e)
. 不过应该没什么区别。不是直接调用编译器,而是调用一个 shell 脚本,它将 stderr/stdout 重定向到一个文件(或使用 保存副本
cc ... |& tee /tmp/cc.log
)以确保复杂设置的任何部分都没有吞下重要信息。