admin管理员组

文章数量:1400409

I'm playing around with node streams and child processes. So I want to emulate next shell mand 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.

I'm playing around with node streams and child processes. So I want to emulate next shell mand 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.
Share Improve this question edited Nov 18, 2014 at 9:21 Paul 27.5k13 gold badges89 silver badges126 bronze badges asked Nov 17, 2014 at 19:02 alexpodsalexpods 48.6k10 gold badges103 silver badges95 bronze badges 10
  • 1 Your code works for me (except with bash instead of ssh). If you do ps au | grep ssh at your shell prompt, do you see output? – mscdex Commented Nov 17, 2014 at 22:44
  • @mscdex Hmm, interesting. With bash mand it works (result is the same as ps au | grep bash). With ssh there is no result. Although ps au | grep ssh give me result: vagrant 11681 0.0 0.1 10464 916 pts/0 S+ 07:54 0:00 grep --color=auto ssh. – alexpods Commented Nov 18, 2014 at 7:57
  • Which userid are these nodejs scripts running under? Nobody or web user perhaps, instead of your login user? – Paul Commented Nov 18, 2014 at 9:17
  • 1 I think there is some kind of timing issue because I can see the output sometimes but not always. – Dongho Yoo Commented Nov 18, 2014 at 9:30
  • @Paul I call this script from console: node pipe_example.js using vagrant (virtual machine with OS Linux Ubuntu 14.04). So user - vagrant, user id - 1000. – alexpods Commented Nov 18, 2014 at 13:00
 |  Show 5 more ments

2 Answers 2

Reset to default 4

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 ment.

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")); 
});

本文标签: javascriptResult of ps augrep ssh different in Nodejs (using spawnpipe) vs shellStack Overflow