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

发布于 2024-11-18 20:58:16 字数 927 浏览 0 评论 0原文

因此,从文档中,我可以 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);
  }
});

So, from the docs, I can grep if a process exists or not. But if it doesn't, I get a returned result of self. I can do this by executing the actual statement, but what about node.js way?! I've tried multiple greps, but maybe somebody has already done this... I just need to add an extra | grep -v |, essentially.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖 2024-11-25 20:58:16

为什么不使用 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);
});

Why not using 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);
});
裂开嘴轻声笑有多痛 2024-11-25 20:58:16

对于上面的答案,您可以尝试

var cmdline = 'ps ax | grep MyProcessThatIsNotRunnin[g]';

grep  = spawn('grep', ['MyProcessThatIsNotRunnin[g]']); 
//should work didn't test though.. :)

方括号是找不到 grep 的技巧。

For the above answer you can try

var cmdline = 'ps ax | grep MyProcessThatIsNotRunnin[g]';

or

grep  = spawn('grep', ['MyProcessThatIsNotRunnin[g]']); 
//should work didn't test though.. :)

The square brackets is a trick to not find the grep.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文