pgrep -P,但对于孙子代而言,不只是子代 [英] pgrep -P, but for grandchildren not just children
本文介绍了pgrep -P,但对于孙子代而言,不只是子代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用:
pgrep -P $$
获取$$的孩子pid.但是我实际上也想要一个孙辈和曾孙辈的名单.
to get the child pids of $$. But I actually want a list of grandchildren and great grandchild too.
我该怎么做?使用常规的编程语言,例如,我们可以使用递归来实现,但是使用bash?也许使用bash函数?
How do I do this tho? With a regular programming language we would do that with recursion for example, but with bash? Perhaps use a bash function?
推荐答案
我最终使用node.js和bash做到了这一点:
I ended up doing this with node.js and bash:
const async = require('async');
const cp = require('child_process');
export const getChildPids = (pid: number, cb: EVCb<Array<string>>) => {
const pidList: Array<string> = [];
const getMoreData = (pid: string, cb: EVCb<null>) => {
const k = cp.spawn('bash');
const cmd = `pgrep -P ${pid}`;
k.stderr.pipe(process.stderr);
k.stdin.end(cmd);
let stdout = '';
k.stdout.on('data', d => {
stdout += String(d || '').trim();
});
k.once('exit', code => {
if (code > 0) {
log.warning('The following command exited with non-zero code:', code, cmd);
}
const list = String(stdout).split(/\s+/).map(v => String(v || '').trim()).filter(Boolean);
if (list.length < 1) {
return cb(null);
}
for (let v of list) {
pidList.push(v);
}
async.eachLimit(list, 3, getMoreData, cb);
});
};
getMoreData(String(pid), err => {
cb(err, pidList);
});
};
这篇关于pgrep -P,但对于孙子代而言,不只是子代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文