Result ps au | grep ssh differs in Node.js (using spawn / pipe) versus shell

I play with node threads and child processes. Therefore, I want to emulate the following shell command using pipes:

ps au | grep ssh

So, I wrote the following code:

var spawn = require('child_process').spawn;
var ps    = spawn('ps',   ['au']);
var grep  = spawn('grep', ['ssh']);

ps.stdout.pipe(grep.stdin);

grep.stdout.on('data', function(data) { console.log(data) });

Then I run it, but nothing happens. What have I done wrong?

PS - I know about:

require('child_process')
   .exec('ps au | grep ssh', function(err, stdout, stderr) { 
       ... 
   }). 

I just play with Node.js and I want to understand what is wrong with this example.

Update 1:
It turned out that with grep the bashprogram works as expected, but with grep sshthere is no result. Although ps au | grep sshit gives me this result:

vagrant 11681 0.0 0.1 10464 916 pts/0 S+ 07:54 0:00 grep --color=auto ssh.
+4
source share
2 answers

ps, , . ps au :

tniese  3251   0,0  0,0  2479028   3004 s000  S+    4:06am   0:00.03 -bash
root    4453   0,0  0,0  2452408    876 s004  R+    4:06pm   0:00.00 ps au

ps au | grep ssh grep, , ssh.

grep , ps , :

tniese  3251   0,0  0,0  2479028   3004 s000  S+    4:06am   0:00.03 -bash
root    4453   0,0  0,0  2452408    876 s004  R+    4:06pm   0:00.00 ps au
tniese  4478   0,0  0,0  2441988    596 s000  R+    4:06pm   0:00.00 grep ssh

grep , , :

tniese  4478   0,0  0,0  2441988    596 s000  R+    4:06pm   0:00.00 grep ssh

, :

var spawn = require('child_process').spawn;
var ps    = spawn('ps',   ['au']);
var grep  = spawn('grep', ['ssh']);

ps.stdout.pipe(grep.stdin);

spawn , ps, ps , , , , , . spawn grep, grep ps , , grep. ps grep. grep ssh, .

Wether grep , . , , , . , ps grep .

, node spawn('ps', ['au']); ps / .

, , .

+2

grep ps, . , . .

var spawn = require('child_process').spawn;
var grep  = spawn('grep', ['ssh']);
var ps    = spawn('ps',   ['au']);

ps.stdout.pipe(grep.stdin);

grep.stdout.on('data', function(data) { 
  console.log(data.toString("utf8")); 
});
+1

All Articles