简体   繁体   English

nodejs exec命令失败,没有有用的错误消息

[英]nodejs exec command failing with no useful error message

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. 'client'是socket.io的回调参数的参数。 'ee' is an instance of EventEmitter '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' is being generated and on running in a shell, is properly executed. /tmp/test.c是一个有效的C代码以及检查目录/ tmp,我发现test.c的是正确的正在生成二进制“测试”和在上壳运行,则适当地执行。 So I dont understand why it is flagging unsuccessful execution. 所以我不明白为什么它会导致执行不成功。 The error object's information is unhelpful too. 错误对象的信息也没有用。 Would appreciate some help/explanation 希望得到一些帮助/解释

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; 看起来不像是一个合适的JSON / JavaScript对象,特别是[Error: Command failed: ]部分; there is at least a comma missing. 至少有一个逗号丢失。

Suggestions: 建议:

  1. Run the command from the command line and check the exit code (use echo $? ). 从命令行运行命令并检查退出代码(使用echo $? )。 If the exit code is != 0, then this means the command "failed" (whatever that might mean). 如果退出代码是!= 0,那么这意味着命令“失败”(无论这意味着什么)。

  2. When the command fails, nodejs says it will put the exit code into e.code (which I'm missing in your output...). 当命令失败时, nodejs表示会将退出代码放入e.code (我在输出中缺少...)。 Find out what happened to this value. 找出这个值发生了什么。

  3. Try if(e !== null) instead of if(e) . 尝试if(e !== null)而不是if(e) Shouldn't make a difference, though. 但是, 不应该有所作为。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM