Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript node.js-访问系统命令的退出代码和stderr_Javascript_Node.js - Fatal编程技术网

Javascript node.js-访问系统命令的退出代码和stderr

Javascript node.js-访问系统命令的退出代码和stderr,javascript,node.js,Javascript,Node.js,下面显示的代码片段非常适合访问系统命令的stdout。是否有某种方法可以修改此代码,以便访问系统命令的退出代码以及系统命令发送给stderr的任何输出 #!/usr/bin/node var child_process = require('child_process'); function systemSync(cmd) { return child_process.execSync(cmd).toString(); }; console.log(systemSync('pwd'));

下面显示的代码片段非常适合访问系统命令的stdout。是否有某种方法可以修改此代码,以便访问系统命令的退出代码以及系统命令发送给stderr的任何输出

#!/usr/bin/node
var child_process = require('child_process');
function systemSync(cmd) {
  return child_process.execSync(cmd).toString();
};
console.log(systemSync('pwd'));

您将需要exec的异步/回调版本。返回了3个值。最后两个是stdout和stderr。另外,
child\u进程
是一个事件发射器。收听退出事件。回调的第一个元素是退出代码。(从语法上看很明显,您需要使用node 4.1.1使下面的代码按编写的方式工作)

请尝试以下操作:

`systemSync('pwd')`

`systemSync('notacommand')`
您将获得:

final exit code is 0
stdout is:/
stderr is:
其次是:

final exit code is 127
stdout is:
stderr is:/bin/sh: 1: notacommand: not found
您还可以使用,因为它返回的信息更多:

return: 
pid <Number> Pid of the child process
output <Array> Array of results from stdio output
stdout <Buffer> | <String> The contents of output[1]
stderr <Buffer> | <String> The contents of output[2]
status <Number> The exit code of the child process
signal <String> The signal used to kill the child process
error <Error> The error object if the child process failed or timed out
返回:
子进程的pid
stdio输出结果的输出数组
stdout |输出的内容[1]
stderr |输出的内容[2]
状态子进程的退出代码
用于终止子进程的信号
如果子进程失败或超时,则返回错误对象

因此,您要查找的退出代码将是
ret.status。

您不需要异步执行。您可以保留execSync功能

用try包装它,传递给catch(e)块的错误将包含您要查找的所有信息

var child_process = require('child_process');

function systemSync(cmd) {
  try {
    return child_process.execSync(cmd).toString();
  } 
  catch (error) {
    error.status;  // Might be 127 in your example.
    error.message; // Holds the message you typically want.
    error.stderr;  // Holds the stderr output. Use `.toString()`.
    error.stdout;  // Holds the stdout output. Use `.toString()`.
  }
};

console.log(systemSync('pwd'));
如果未引发错误,则:

  • 状态保证为0
  • stdout是函数返回的内容
  • stderr几乎肯定是空的,因为它是成功的

在极少数情况下,命令行可执行文件返回一个stderr,但退出时状态为0(成功),如果要读取它,则需要异步函数。

>您需要异步/回调版本的exec。我实际上是在尝试同步执行。我已经了解到,我显然是在用这种方法抓住救命稻草。异步方法非常有效。谢谢您的输入。@user3311045-如果您接受我的答案,那就太棒了。但是如果你想支持一个能想出如何使用同步选项的人,那就太酷了。我试图通过
execSync
来解决这个问题,但我认为没有办法。感谢lance——创建了这个npm模块——shelljs.exec——它将您在这里列出的所有逻辑封装到一个漂亮的小数据包中——干杯:DI会说,进程返回0的退出代码,但在stderr中也包含输出,这实际上是很常见的。stderr经常用于调试信息和其他与命令输出不直接相关的信息。@IshaanGandhi如果是这样,我想确认行为在哪个版本中发生了更改。我还想知道是否有一些配置可以传递以启用这种预期行为。我个人不再从事任何节点项目,以确认我自己的错误。我认为它应该在总体上起作用。当我使用节点8在electron中运行命令时,不会抛出任何错误,但会从节点repl抛出一个错误。可能electron中存在错误。需要注意的一点是,如果超过maxBuffer,则如果父进程杀死子进程,execSync将抛出错误,但
错误。状态将为null
var child_process = require('child_process');

function systemSync(cmd) {
  try {
    return child_process.execSync(cmd).toString();
  } 
  catch (error) {
    error.status;  // Might be 127 in your example.
    error.message; // Holds the message you typically want.
    error.stderr;  // Holds the stderr output. Use `.toString()`.
    error.stdout;  // Holds the stdout output. Use `.toString()`.
  }
};

console.log(systemSync('pwd'));