Kunok
Kunok

Reputation: 8759

Node child_process mute, read and handle children logs within parent

I have a parent script that spawns children nodes.

let child = child_process.fork('foo.js', ['param1', param2])
child.on('message', data => {
//  console.log(data);
})
child.on('exit', code => {
  resolve(true)
})

Originally I thought that child.on('message') event listener catches and handles message but that's not the case here. Children simply uses parent process.stdout.write function and log stuff to parent console by default.

Upvotes: 0

Views: 422

Answers (1)

rlemon
rlemon

Reputation: 17666

you can set silent to true on the child fork and listen for the stdout pipe.

fork('./foo.js',{silent: true})
    .stdout.on('data', data => console.log(data.toString()));

Upvotes: 3

Related Questions