簡體   English   中英

nodejs exec命令失敗,沒有有用的錯誤消息

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

這是要執行的代碼



    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)是

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

being generated and on running in a shell, is properly executed. /tmp/test.c是一個有效的C代碼以及檢查目錄/ tmp,我發現test.c的是正確的正在生成二進制“測試”和在上殼運行,則適當地執行。 所以我不明白為什么它會導致執行不成功。 錯誤對象的信息也沒有用。 希望得到一些幫助/解釋

我有點擔心控制台的輸出。

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

看起來不像是一個合適的JSON / JavaScript對象,特別是[Error: Command failed: ]部分; 至少有一個逗號丟失。

建議:

  1. 從命令行運行命令並檢查退出代碼(使用echo $? )。 如果退出代碼是!= 0,那么這意味着命令“失敗”(無論這意味着什么)。

  2. 當命令失敗時, nodejs表示會將退出代碼放入e.code (我在輸出中缺少...)。 找出這個值發生了什么。

  3. 嘗試if(e !== null)而不是if(e) 但是, 不應該有所作為。

  4. 不要直接調用編譯器,而是調用一個shell腳本,它將stderr / stdout重定向到一個文件(或使用cc ... |& tee /tmp/cc.log保存一個副本),以確保復雜設置的任何部分都不會重要信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM