nodejs exec command failing with no useful error message

自闭症网瘾萝莉.ら 提交于 2020-07-05 06:35:31

问题


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

Coming to the problem. On running the code, the callback says that the command was unsuccessful. console.log(e, stdout, stderr) is

{ [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. So I dont understand why it is flagging unsuccessful execution. The error object's information is unhelpful too. Would appreciate some help/explanation


回答1:


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.

Suggestions:

  1. 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).

  2. When the command fails, nodejs says it will put the exit code into e.code (which I'm missing in your output...). Find out what happened to this value.

  3. Try if(e !== null) instead of 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.



来源:https://stackoverflow.com/questions/14319724/nodejs-exec-command-failing-with-no-useful-error-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!