树树
注: 这是叉子 树树 国家防范机制一揽子计划,包括一些修正和额外安排.
有时你无法像预期的那样杀死孩子的过程,这是UNIX的一个特征.
在UNIX中,一个进程可能通过使用出站呼叫而终止,它的主进程可能通过使用等待系统调用来等待该事件. 等待系统调用返回终止孩子的过程标识符,这样父母就可以告诉哪些孩子已经终止。 但是,如果父母终止,所有孩子 都指定作为他们的新父母 加入过程。 因此,儿童仍然有一个家长来收集其状况和被处决的统计数据. (出自"操作系统概念"
解决方案:使用 ps-tree 获取所有进程 child_process 可能已经开始了,所以可以全部终止.
var cp = require('child_process'),
psTree = require('ps-tree');
var child = cp.exec("node -e 'while (true);'", function () {...});
// This will not actually kill the child it will kill the `sh` process.
child.kill();
怎么样? 因为执行官实际上是这样工作的:
function exec (cmd, cb) {
spawn('sh', ['-c', cmd]);
...
}
sh 开始解析命令字符串并启动进程,等待它们终止,但 exec 返回带有 pid 的处理对象 sh 进程.
但是,因为它是在 wait 模式杀死它不会杀死孩子.
使用 ps-tree 像这样:
var cp = require('child_process'),
psTree = require('ps-tree');
var child = cp.exec("node -e 'while (true);'", function () { /*...*/ });
psTree(child.pid, function (err, children) {
cp.spawn('kill', ['-9'].concat(children.map(function (p) { return p.PID })));
});
如果你喜欢跑 脚本 从命令行中, 使用 : node ./bin/ps-tree.js
跨平台支持
这个 ps-tree 模块在 *nix vs. Windows 上表现不同,方法是生成不同的程序并解析其输出. 这是基于 process.platform 而不是检查是否 ps 兼容程序存在于系统中.
*没有
- " 需要被剥去 "
$ ps -A -o comm,ppid,pid,stat
COMMAND PPID PID STAT
bbsd 2899 16958 Ss
watch <defunct> 1914 16964 Z
ps 20688 16965 R+
窗口
wmic PROCESS WHERE ParentProcessId=4604 GET Name,ParentProcessId,ProcessId,Status)- 头列的顺序固定了
> wmic PROCESS GET Name,ProcessId,ParentProcessId,Status
Name ParentProcessId ProcessId Status
System Idle Process 0 0
System 0 4
smss.exe 4 228