Result Of Ps Au | Grep Ssh Different In Node.js (using Spawn/pipe) Vs Shell
Solution 1:
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.
Solution 2:
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"));
});
Post a Comment for "Result Of Ps Au | Grep Ssh Different In Node.js (using Spawn/pipe) Vs Shell"