无法使用 execFileSync 获取 stdout 或 stderr

时间:2021-03-12 15:23:58

标签: node.js

我知道当我直接在命令行上运行时会产生一些输出:

bash-5.0# gvpr -V
Segmentation fault
bash-5.0# echo $?
139

这里是 execFileSync 的样子:

bash-5.0# node
Welcome to Node.js v14.15.0.
Type ".help" for more information.
> var {execFileSync} = require('child_process')
undefined
> execFileSync('gvpr', ['-V'])
Uncaught Error: Command failed: gvpr -V
    at checkExecSyncError (child_process.js:616:11)
    at execFileSync (child_process.js:634:15) {
  status: null,
  signal: 'SIGSEGV',
  output: [ null, <Buffer >, <Buffer > ],
  pid: 517,
  stdout: <Buffer >,
  stderr: <Buffer >
}

我们可以在上面的输出中看到一些 <Buffer >,所以我希望得到它们的内容,但我所有的尝试都返回了一个空字符串。

> try { execFileSync('gvpr', ['-V']) } catch (e) { console.log('e.output[1]=', e.output[1].toString()) }
e.output[1]= 
undefined
> try { execFileSync('gvpr', ['-V']) } catch (e) { console.log('e.output[2]=', e.output[2].toString()) }
e.output[2]= 
undefined
> try { execFileSync('gvpr', ['-V']) } catch (e) { console.log('e.stdout=', e.stdout.toString()) }
e.stdout= 
undefined
> try { execFileSync('gvpr', ['-V']) } catch (e) { console.log('e.stderr=', e.stderr.toString()) }
e.stderr= 
undefined

我做错了什么?

0 个答案:

没有答案
相关问题