Closed ctag closed 9 years ago
STDOUT
from grep
is being buffered. If you use --line-buffered
with grep
, things work as you expect:
grep = spawn('grep', ['--line-buffered', 'ssh']);
That is the solution for me, thank you!
Since I don't intend to use grep
in my end script, I took this and tried to find a more general solution. The unbuffer
program, on my distro bundled with expect
, appears to do this:
grep = spawn('unbuffer', ['grep', 'ssh']);
And for a small set of programs I tried it on, output arrives at each newline.
For me, I had set an environment variable in the spawn
options, like:
spawn(command, {
env: {
BROWSER: "none"
}
}
In Windows, other environment variables seemed to be carried along. But in WSL, they were not, so the program was not working. The solution was to spread the other environment variables such as:
spawn(command, {
env: {
...process.env,
BROWSER: "none"
}
}
which worked in both Windows and WSL envs.
I'd like to have a nodejs script interact with a child process via standard input and output. I've tried setting this up with what the documentation provides, but I've arrived at a deadlock where what's happening doesn't match what's I believe is documented.
What I see in the documentation:
Reading each section leads me to:
The section for spawn describes access to an
stdio
option. Looking further up the page, I read that the child's stdin and stdout both default to a writeable or readable stream.OK, I don't entirely know what a stream is in the context of Node, so looking at the stream doc page:
Cool, and then since I've already looked at the child-process.spawn examples, I know that
.on('data'...
is important. It's listed right below:This looks pretty straightforward, I want
spawn
. This child process of mine will persist for a while, and I want access to the output as it appears. Sounds like a good match.Running the provided example:
Great, time to become familiar with the
.spawn
operator. To do so, I'll run the providedps ax | grep ssh
example:Output:
It works!
Running a modified example:
But I notice that grep exits pretty quickly. I want my program to persist, so I'm going to modify the example to leave grep running for a while longer.
Output:
No output? Why would that be the case? Closing grep's stdin will cause it to exit, leaving it open keeps me from seeing the output I want. All in all, how is this different from the functionality of
.exec
?I tried to link pretty closely to the documentation because I'm confident that Node is working as expected and that I've misread something that brought me here.