Javascript node.js - 访问系统命令的退出代码和标准错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32874316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:18:33  来源:igfitidea点击:

node.js - Accessing the exit code and stderr of a system command

javascriptnode.js

提问by user3311045

The code snippet shown below works great for obtaining access to the stdout of a system command. Is there some way that I can modify this code so as to also get access to the exit code of the system command and any output that the system command sent to stderr?

下面显示的代码片段非常适合获取对系统命令的标准输出的访问。有什么方法可以修改此代码,以便还可以访问系统命令的退出代码以及系统命令发送到 stderr 的任何输出?

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

回答by lance.dolan

You do not need to do it Async. You can keep your execSync function.

你不需要做异步。您可以保留 execSync 功能。

Wrap it in a try, and the Error passed to the catch(e) block will contain all the information you're looking for.

将其包装在 try 中,传递给 catch(e) 块的 Error 将包含您要查找的所有信息。

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'));

If an error is NOT thrown, then:

如果没有抛出错误,则:

  • status is guaranteed to be 0
  • stdout is what's returned by the function
  • stderr is almost definitely empty because it was successful.
  • status 保证为 0
  • stdout 是函数返回的内容
  • stderr 几乎肯定是空的,因为它成功了。

In the rare event the command line executable returns a stderr and yet exits with status 0 (success), and you want to read it, you will need the async function.

在极少数情况下,命令行可执行文件返回一个标准错误,但以状态 0(成功)退出,并且您想要读取它,您将需要异步函数。

回答by bbuckley123

You will want the async/callback version of exec. There are 3 values returned. The last two are stdout and stderr. Also, child_processis an event emitter. Listen for the exitevent. The first element of the callback is the exit code. (Obvious from the syntax, you'll want to use node 4.1.1 to get the code below to work as written)

您将需要 exec 的异步/回调版本。返回了 3 个值。最后两个是标准输出和标准错误。此外,child_process是一个事件发射器。监听exit事件。回调的第一个元素是退出代码。(从语法中可以看出,您需要使用节点 4.1.1 来使下面的代码按编写的方式工作)

const child_process = require("child_process")
function systemSync(cmd){
  child_process.exec(cmd, (err, stdout, stderr) => {
    console.log('stdout is:' + stdout)
    console.log('stderr is:' + stderr)
    console.log('error is:' + err)
  }).on('exit', code => console.log('final exit code is', code))
}

Try the following:

请尝试以下操作:

`systemSync('pwd')`

`systemSync('notacommand')`

And you will get:

你会得到:

final exit code is 0
stdout is:/
stderr is:

Followed by:

其次是:

final exit code is 127
stdout is:
stderr is:/bin/sh: 1: notacommand: not found

回答by infografnet

You may also use child_process.spawnSync(), as it returns much more:

您也可以使用child_process.spawnSync(),因为它返回更多:

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

So the exit code you're looking for would be ret.status.

所以你要找的退出代码是 ret.status.