[英]Node child process event listen
I use the node child_process API 我使用节点child_process API
https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options
var child = child_process.spawn(cmd, val, options);
from the child I use the following 从孩子那里我使用以下
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
Can I add inside those pipe event some code inside like console.log? 我可以在这些管道事件中添加一些诸如console.log之类的代码吗?
like for example maybe with prototype 例如,也许有原型
child.on('error', function(err) {
console.log(err);
});
update 更新
What I need that is to listen to this childProcess.stderr.pipe(process.stderr);
我需要的是听这个
childProcess.stderr.pipe(process.stderr);
and In case I got and error do process.exit(1)
并且如果我遇到错误并执行
process.exit(1)
when I try something like I got error 当我尝试出现错误时
child.stderr.pipe(function () {
console.log("im here");
process.stderr;
process.exit(1);
}
);
UPDATE2 更新2
I try the following 我尝试以下
var child = child_process.spawn(cmd, value, opt);
child.stdout.on('data', function (data) {
console.log("IM HERE");
console.log('data' + data);
});
child.stderr.on('data', function (data) {
console.log("IM HERE");
console.log('test: ' + data);
reject(data);
});
child.on('close', function (code) {
console.log("IM HERE");
console.log("close");
});
child.on('error', function (err) {
console.log("IM HERE");
console.log(err);
});
child.stderr.on('error', function (err) {
console.log("IM HERE");
console.log("my Erorr");
process.stderr.emit('error', err);
});
child.stdout.on('data', function (buf) {
console.log("IM HERE");
console.log('buf receive');
console.log(buf.toString());
});
//Just when I add the following I see the error in the log //当我添加以下内容时,我会在日志中看到错误
child.stderr.pipe(process.stderr)
Non of the console.log("im here") is printed in case of error
如果出现错误,将输出console.log(“ im here”)中的非
I need somehow to listen to this pipe or maybe to extend somehow the child.stderr.pipe(process.stderr), what I need is to do process.exit(1)
in case I got error from the code statement above... 我需要某种方式来侦听此管道,或者可能以某种方式扩展child.stderr.pipe(process.stderr),以防万一我从上面的代码声明中出错了,我需要做
process.exit(1)
...
Maybe with javascript prototype but not sure how to do that... 也许使用JavaScript原型,但不确定如何做到这一点...
Please assist Im stuck and I know this is not simple... 请协助我卡住,我知道这并不简单...
This works for me: 这对我有用:
var child_process = require('child_process');
var cmd = 'ls';
var value = ['-z', '/usr'];
var opt = { };
var child = child_process.spawn(cmd, value, opt);
child.stdout.on('data', function (data) {
console.log("IM HERE");
console.log('data' + data);
});
child.stderr.on('data', function (data) {
console.log("IM HERE - Error");
console.log('test: ' + data);
});
child.on('close', function (code) {
console.log("IM HERE");
console.log("close");
});
Console output: 控制台输出:
/*
IM HERE - Error
test: ls: invalid option -- 'z'
Try 'ls --help' for more information.
IM HERE
close
*/
The issue in your side, maybe the command you're spawning doesn't use stderr? 您身边的问题,也许您生成的命令没有使用stderr?
Update 更新资料
If I add process.exit()
如果我添加
process.exit()
var child_process = require('child_process');
var cmd = 'ls';
var value = ['-z', '/usr'];
var opt = { };
var child = child_process.spawn(cmd, value, opt);
child.stdout.on('data', function (data) {
console.log("IM HERE");
console.log('data' + data);
});
child.stderr.on('data', function (data) {
console.log("IM HERE - Error");
console.log('test: ' + data);
process.exit(1); // <<<< this works as expected and exit the process asap
});
child.on('close', function (code) {
console.log("IM HERE");
console.log("close");
});
The ouput is slightly different (no close event from the child) 输出量略有不同(孩子没有亲密事件)
/*
IM HERE - Error
test: ls: invalid option -- 'z'
Try 'ls --help' for more information.
*/
You can "play" with the code here: https://tonicdev.com/shanshan/cp-spawn-piping 您可以在此处使用代码“播放”: https : //tonicdev.com/shanshan/cp-spawn-piping
You can subscribe to the data
event(stdout or stderr) and listen it 您可以订阅
data
事件(stdout或stderr)并收听
child.stdout.on('data', function (buf) {
console.log(buf.toString());
});
For example 例如
const spawn = require('child_process').spawn;
const child = spawn('ping', ['google.com']);
child.stdout.on('data', function (buf) {
console.log('buf receive');
console.log(buf.toString());
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.