# @apify/ps-tree 中文文档

规范地址：https://npmcn.com/package/%40apify/ps-tree/

版本：1\.2\.0
npm 元数据更新时间：2026-02-24T16:26:15.019Z
中文译文更新时间：2026-11-06T10:03:31.456Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40apify/ps-tree
原文对照：https://npmcn.com/package/%40apify/ps-tree/?lang=original
项目主页：https://github.com/apifytech/ps-tree
源码仓库：https://github.com/apifytech/ps-tree

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：10.5 kB，5 个文件
- 模块格式：CommonJS
- TypeScript 类型：未声明
- Node.js 要求：\>= 0\.10
- 运行依赖：1 个
- 首次发布：2018-08-17T13:56:47.504Z
- 最新版本发布：2023-03-01T13:23:07.499Z
- 近 30 天下载量：902138（2026-10-06 至 2026-11-04，npm 官方下载统计）
- 已发布版本数：6（近 12 个月 0 个）

## 中文简介

把所有的小孩都带走

## 安装

```sh
npm install @apify/ps-tree
```

也可以使用：`pnpm add @apify/ps-tree`、`yarn add @apify/ps-tree`、`bun add @apify/ps-tree`

## README

# 树树

[![Build Status](https://travis-ci.org/indexzero/ps-tree.svg)](https://travis-ci.org/indexzero/ps-tree) [![Code Climate](https://codeclimate.com/github/indexzero/ps-tree/badges/gpa.svg)](https://codeclimate.com/github/indexzero/ps-tree) [![Test Coverage](https://codeclimate.com/github/indexzero/ps-tree/badges/coverage.svg)](https://codeclimate.com/github/indexzero/ps-tree) [![npm Version](https://img.shields.io/npm/v/ps-tree.svg)](https://www.npmjs.com/package/ps-tree) [![Node.js Version](https://img.shields.io/node/v/ps-tree.svg?style=flat)](http://nodejs.org/download/) [![Dependency Status](https://david-dm.org/indexzero/ps-tree.svg)](https://david-dm.org/indexzero/ps-tree)

注: 这是叉子 [树树](https://www.npmjs.com/package/ps-tree) 国家防范机制一揽子计划,包括一些修正和额外安排.

有时你无法像预期的那样杀死孩子的过程,这是UNIX的一个特征.

> 在UNIX中,一个进程可能通过使用出站呼叫而终止,它的主进程可能通过使用等待系统调用来等待该事件. 等待系统调用返回终止孩子的过程标识符,这样父母就可以告诉哪些孩子已经终止。 但是,如果父母终止,所有孩子 都指定作为他们的新父母 加入过程。 因此,儿童仍然有一个家长来收集其状况和被处决的统计数据. (出自"操作系统概念"

解决方案:使用 `ps-tree` 获取所有进程 `child_process` 可能已经开始了,所以可以全部终止.

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

怎么样? 因为执行官实际上是这样工作的:

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

`sh` 开始解析命令字符串并启动进程,等待它们终止,但 `exec` 返回带有 pid 的处理对象 `sh` 进程. 但是,因为它是在 `wait` 模式杀死它不会杀死孩子.

使用 `ps-tree` 像这样:

```js
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` 兼容程序存在于系统中.

#### \*没有

1.  " 需要被剥去 "

```bash
$ ps -A -o comm,ppid,pid,stat
COMMAND          PPID   PID STAT
bbsd             2899 16958 Ss
watch <defunct>  1914 16964 Z
ps              20688 16965 R+
```

### 窗口

1.  `wmic PROCESS WHERE ParentProcessId=4604 GET Name,ParentProcessId,ProcessId,Status)`
2.  头列的顺序固定了

```shell
> wmic PROCESS GET Name,ProcessId,ParentProcessId,Status
Name                          ParentProcessId  ProcessId   Status
System Idle Process           0                0
System                        0                4
smss.exe                      4                228
```

### 麻省理工学院

## 常见问题

### @apify/ps-tree 是什么？

把所有的小孩都带走

### 如何安装 @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 和提交记录判断。

