简体   繁体   中英

How to pipe the output forked/cluster processes into the master stdout/stderr

When doing worker = cluster.fork() I would like to be able to forward the stdout and stderr streams into the master Node.js process.

How can I do that?

I tried doing:

worker = cluster.fork();
worker.process.stdout.pipe(process.stdout)
//             ^ this is null

A workaround would be to use messages, but I want to be able to stream content.

worker.on("message", function(msg){
  console.log("Master says:" + msg);
});
...
worker.send({message:'hello'});

How can I access the stdout of the forked process/cluster?

please see the code below

for the master process:

const cluster = require('cluster');
cluster.settings.stdio=[0, 1, 2, 'ipc'];
cluster.settings.silent = true; //Whether or not to send output to parent's stdio. Default: false. 
cluster.settings.exec = './hello' //the forked process's file path
const childW = cluster.fork();
const child = childW.process;

for the forked process: ./hello.js

console.log('hello')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM