Node.js grep 进程(但不包括 self)

标签 node.js grep

因此,从文档中,我可以 grep 进程是否存在。但如果没有,我会得到 self 的返回结果。我可以通过执行实际的语句来做到这一点,但是node.js 的方式怎么样?!我已经尝试了多个 grep,但也许有人已经这样做了......我只需要添加一个额外的 | grep -v | ,本质上。

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


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

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

ps.on('exit', 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.toString());
});

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

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

最佳答案

为什么不使用 child_prosses.exec

var cmdline = 'ps ax | grep -v grep | grep MyProcessThatIsNotRunning';
require('child_process').exec(cmdline, function (error, stdout, stderr) {
    if (error)
    {
        console.error(error); process.exit(1);
    }
    // parse 'ps ax | grep -v grep | grep MyProcessThatIsNotRunning' result here
    console.log(stdout);
});

关于Node.js grep 进程(但不包括 self),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6588863/

相关文章:

text - 使用 awk 或 sed 在但不包括页眉和页脚之间获取文本

linux - 查找具有大小和时间并按时间戳排序的命令

node.js - 如何仅使用ASAR文件手动构建Electron应用程序?

javascript - 如何用Electron检测蓝牙适配器?

node.js - Dialogflow Webhook UpdatePermissions 自定义提示

python - 通过搜索unix中的所有目录来编译值列表

linux - 如何从单行输出中grep和输出由空格分隔的字符串 block

awk - 使用 grep 从文件中匹配 python 多行表达式字符串?

node.js - npm 命令 - 读取错误 : EISDIR: illegal operation on a directory,

DocumentDB 的 Node.js 代理服务