Say I have a named pipe on linux:
mkfifo lk.log
From the command line, I can do this to print out anything written to the name pipe file.
node monitor.js < lk.log
and pretend this is what the script looks like
// monitor.js
process.stdin.resume();
process.stdin.setEncoding('utf8');
// read data from stdin
process.stdin.on('data', function(chunk) {
console.log(chunk);
});
How could I do this within node using child_process.spawn?
child_process.spawn('node', ['monitor.js'])...
The easiest way would be to use exec():
var exec = require('child_process').exec;
exec('node monitor.js < lk.log', function(err, stdout, stderr) {
...
});
A more elaborate way would be to open the named pipe in node and pass it as stdin to the process you're spawning (see the option stdio for spawn).
The answer is to use fs.open and the stdio options in child_process.spawn as such:
var spawn = require('child_process').spawn;
var fd_stdin = fs.openSync('lk.log', 'r');
spawn('node', ['monitor.js'], {
stdio: [fd_stdin, 1, 2];
});
From Ben Noordhuis (Core Node contributor) - 10/11/11
Windows has a concept of named pipes but since you mention
mkfifoI assume you mean UNIX FIFOs.We don't support them and probably never will (FIFOs in non-blocking mode have the potential to deadlock the event loop) but you can use UNIX sockets if you need similar functionality.
https://groups.google.com/d/msg/nodejs/9TvDwCWaB5c/udQPigFvmgAJ
For unix sockets, see: http://stackoverflow.com/a/18226566/977939