日期:2014-05-16  浏览次数:20379 次

node.js child_process simple operctor 2
[node.js] clied_process

simple operctor : ps -ax | grep ssh


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

ps.stdout.on('data', function (data) {
    grep.stdin.write(data);
});

ps.stderr.on('data', function (data) {
    console.log('ps stderr: ' + data);
});

ps.on('close', function (code) {
    if (code !== 0) {
        console.log('ps process exited with code ' + code);
    }
    grep.stdin.end();
});

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

grep.stderr.on('data', function (data) {
    console.log('grep stderr: ' + data);
});

grep.on('close', function (code) {
    if (code !== 0) {
        console.log('grep process exited with code ' + code);
    }
});