Nodejs:child_process.exec 命令 return 什么都没有时出错
Nodejs: child_process.exec get error when command return nothing
在nodejs中使用child_process.exec时,命令return什么都没有时会报错。我执行以下操作:
const {exec} = require('child_process');
const cmd = `ps -ef | grep -v grep | grep abc123.py`;
exec(cmd, (err, stdout, stderr) => {
if(err) {
console.error(`__get error: ${stderr}`);
return;
}
console.log(stdout);
return;
})
因为'abc123.py'不是运行ning,直接运行这个命令就return没什么。但是这段代码得到了这个:
__get error:
我在 Node 8.10.0 和 10.16.0 上遇到了这个错误。有什么我忽略的吗?
您尝试执行不存在的脚本,因此您的 ps -ef | grep -v grep | grep abc123.py
return 1 作为退出代码并且不向 stderr 写入任何内容。从 Nodejs.org 我们知道
Any exit code other than 0 is considered to be an error.
所以,您的代码工作正常。
在nodejs中使用child_process.exec时,命令return什么都没有时会报错。我执行以下操作:
const {exec} = require('child_process');
const cmd = `ps -ef | grep -v grep | grep abc123.py`;
exec(cmd, (err, stdout, stderr) => {
if(err) {
console.error(`__get error: ${stderr}`);
return;
}
console.log(stdout);
return;
})
因为'abc123.py'不是运行ning,直接运行这个命令就return没什么。但是这段代码得到了这个:
__get error:
我在 Node 8.10.0 和 10.16.0 上遇到了这个错误。有什么我忽略的吗?
您尝试执行不存在的脚本,因此您的 ps -ef | grep -v grep | grep abc123.py
return 1 作为退出代码并且不向 stderr 写入任何内容。从 Nodejs.org 我们知道
Any exit code other than 0 is considered to be an error.
所以,您的代码工作正常。