@apify/ps-tree

中文文档
1.2.0发布于 2023/03/01MITCommonJS近 30 天下载 90.2万

Get all children of a pid

原始使用文档
文档目录3 节
  1. Cross Platform support
  2. Windows
  3. LICENSE: MIT

ps-tree

Build Status Code Climate Test Coverage npm Version Node.js Version Dependency Status

NOTE: This is a fork of ps-tree NPM package, with some fixes and extras.

Sometimes you cannot kill child processes like you would expect, this a feature of UNIX.

in UNIX, a process may terminate by using the exit call, and it's parent process may wait for that event by using the wait system call. the wait system call returns the process identifier of a terminated child, so that the parent tell which of the possibly many children has terminated. If the parent terminates, however, all it's children have assigned as their new parent the init process. Thus, the children still have a parent to collect their status and execution statistics. (from "operating system concepts")

Solution: use ps-tree to get all processes that a child_process may have started, so that they may all be terminated.

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

wtf? it's because exec actually works like this:

function exec (cmd, cb) {
  spawn('sh', ['-c', cmd]);
  ...
}

sh starts parses the command string and starts processes, and waits for them to terminate, but exec returns a process object with the pid of the sh process. However, since it is in wait mode killing it does not kill the children.

Use ps-tree like this:

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

If you prefer to run psTree from the command line, use: node ./bin/ps-tree.js

Cross Platform support

The ps-tree module behaves differently on *nix vs. Windows by spawning different programs and parsing their output. This is based on process.platform and not on checking to see if a ps compatible program exists on the system.

*nix

  1. " " need to be striped
$ ps -A -o comm,ppid,pid,stat
COMMAND          PPID   PID STAT
bbsd             2899 16958 Ss
watch <defunct>  1914 16964 Z
ps              20688 16965 R+

Windows

  1. wmic PROCESS WHERE ParentProcessId=4604 GET Name,ParentProcessId,ProcessId,Status)
  2. The order of head columns is fixed
> wmic PROCESS GET Name,ProcessId,ParentProcessId,Status
Name                          ParentProcessId  ProcessId   Status
System Idle Process           0                0
System                        0                4
smss.exe                      4                228

LICENSE: MIT

版本与发布记录

@apify/ps-tree 共发布 6 个版本,首次发布于 2018/08/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.2.0latest2023/03/013 年前
1.1.42021/05/045 年前
1.1.32018/11/277 年前
1.1.22018/09/288 年前
1.1.12018/08/178 年前
1.1.02018/08/178 年前

关于 @apify/ps-tree 的常见问题

@apify/ps-tree 是什么?

Get all children of a pid

如何安装 @apify/ps-tree?

在项目目录运行 npm install @apify/ps-tree,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 ps-tree,可以用 npx ps-tree 直接运行。

@apify/ps-tree 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/apify__ps-tree 是否存在。

@apify/ps-tree 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@apify/ps-tree 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 0.10。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@apify/ps-tree 可以商用吗?

@apify/ps-tree 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@apify/ps-tree 的下载量有多少?

根据 npm 官方下载统计,@apify/ps-tree 在 2026/10/06 – 2026/11/04 这 30 天内被下载 902,138 次,日均约 30,071 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@apify/ps-tree 还在维护吗?

最新版本 1.2.0 发布于 2023/03/01(3 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/08/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。