Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Result of ps au | grep ssh different in Node.js (using spawn/pipe) vs shell

I'm playing around with node streams and child processes. So I want to emulate next shell command with pipes:

ps au | grep ssh

So I wrote next 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 did I do wrong?

P.S. - I know about:

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

I'm just playing around with Node.js, and I want to understand what's wrong with this example.

Update 1:
It appeared that with grep bash program works as expected, but with grep ssh there is no result. Although ps au | grep ssh gives me this result:

vagrant 11681 0.0 0.1 10464 916 pts/0 S+ 07:54 0:00 grep --color=auto ssh.
like image 671
alexpods Avatar asked Nov 17 '14 19:11

alexpods


2 Answers

When you call ps it will list all currently running processes matching the passed options. Which might look for ps au something like this:

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

When you call ps au | grep ssh in the shell grep will filter that result to only show the lines containing ssh.

If the grep is launched by the shell before ps creates its listing then the output before the filtering would be:

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

The grep process will match its own entry as it contains the passed parameters, so the filtered result would be:

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

Lets look what is happening with your code:

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

ps.stdout.pipe(grep.stdin);

With spawn you tell the OS to start the process ps, ps does not need to wait to run until the output could be piped to anyplace but could start before that, it might only be forced to wait when it tries to write to the its output stream. Then your spawn grep, but at the time grep is launched ps might already created the process listing internally and cause of that it does not contain the grep process. The output of ps is then passed to grep. But as this output is missing grep ssh it won't show that line.

Wether grep will appear in your listing or not is highly OS dependent. Generally you should assume that it is random if it is listed or not. Or you would need to wait until ps exits and launch grep after that.

You need to always keep in mind that current OS have preemptive multitasking and that the scheduler might pause node right after spawn('ps', ['au']); and continue the process right after ps created/requested the listing.

I hope this explanation is a bit clearer then my comment.

like image 100
t.niese Avatar answered Nov 03 '22 19:11

t.niese


I've spawned grep before ps and now it works well. I think it must be a timing issue. Try this.

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")); 
});
like image 44
Dongho Yoo Avatar answered Nov 03 '22 19:11

Dongho Yoo