Node.js grep 进程(但不包括 self)
因此,从文档中,我可以 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不使用 child_prosses.exec ?
Why not using child_prosses.exec ?
对于上面的答案,您可以尝试
或
方括号是找不到 grep 的技巧。
For the above answer you can try
or
The square brackets is a trick to not find the grep.