witr - 追踪进程启动链的利器

GitHub: pranshuparmar/witr
Stars: 14k+

为什么需要 witr?

作为运维或开发者,你是否遇到过这些问题:

  • 某个端口被占用,但不知道是哪个进程?
  • 发现一个陌生进程在运行,想知道它是怎么被启动的?
  • 生产环境有异常进程,需要追溯它的启动链路?

传统做法是用 pslsofsssystemctl 等工具组合查询,然后手动关联信息。这个过程繁琐且容易遗漏。

witr 就是为了解决这个问题而生的 —— 它能直接告诉你:这个进程是从哪里来的,是谁启动的,完整的启动链是什么


witr 是什么?

witr 的名字来自它的核心问题:**”Why is this running?”**

它是一个用 Go 语言编写的命令行工具,能够:

  • 追踪进程的完整启动链(从 init/systemd 一直到目标进程)
  • 按进程名、PID、端口、文件等多种方式查询
  • 显示进程的工作目录、环境变量、监听端口等信息
  • 提供交互式 TUI 界面,实时探索系统状态

核心功能

1. 进程启动链追踪

最核心的功能 —— 显示一个进程是从哪里启动的:

1
witr node

输出示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Target      : node

Process : node (pid 14233)
User : pm2
Command : node index.js
Started : 2 days ago (Mon 2025-02-02 11:42:10 +05:30)
Restarts : 1

Why It Exists :
systemd (pid 1) → pm2 (pid 5034) → node (pid 14233)

Source : pm2

Working Dir : /opt/apps/expense-manager
Git Repo : expense-manager (main)
Listening : 127.0.0.1:5001

一目了然:这个 node 进程是由 pm2 启动的,而 pm2 又是由 systemd 管理的。

2. 多种查询方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 按进程名
witr nginx

# 按 PID
witr --pid 1234

# 按端口号
witr --port 5000

# 按文件(找出谁在使用这个文件)
witr --file /var/lib/dpkg/lock

# 组合查询
witr nginx --port 5432 --pid 1234

3. 树形视图

查看进程的父子关系:

1
witr --pid 143895 --tree
1
2
3
4
5
6
7
8
9
10
systemd (pid 1)
└─ init-systemd (pid 2)
└─ SessionLeader (pid 143858)
└─ Relay (pid 143859)
└─ bash (pid 143860)
└─ sh (pid 143886)
└─ node (pid 143895)
├─ node (pid 143930)
├─ node (pid 144189)
└─ node (pid 144234)

4. 交互式 TUI

直接运行 witr 进入交互模式:

  • 实时进程列表,支持排序和过滤
  • 端口视图,查看端口占用
  • 进程详情,查看完整信息
  • 支持鼠标操作
  • 可以直接发送信号(Kill、Terminate、Pause、Resume)

5. JSON 输出

方便脚本处理:

1
witr nginx --json

安装

witr 支持几乎所有主流安装方式

Homebrew (macOS/Linux)

1
brew install witr

快速安装脚本

1
2
3
4
5
# Linux/macOS/FreeBSD
curl -fsSL https://raw.githubusercontent.com/pranshuparmar/witr/main/install.sh | bash

# Windows (PowerShell)
irm https://raw.githubusercontent.com/pranshuparmar/witr/main/install.ps1 | iex

其他方式

  • Conda: conda install -c conda-forge witr
  • Winget: winget install -e --id PranshuParmar.witr
  • NPM: npm install -g @pranshuparmar/witr
  • Arch AUR: yay -S witr-bin
  • Go: go install github.com/pranshuparmar/witr/cmd/witr@latest

实用场景

场景一:端口占用排查

1
witr --port 3000

立马知道是谁在占用端口,怎么启动的。

场景二:异常进程调查

发现一个陌生进程名,直接查:

1
witr strange_process

看启动链就知道是哪个服务或脚本启动的。

场景三:容器内进程追踪

在容器环境中,witr 能穿透显示真实的启动来源。

场景四:系统服务调试

1
witr --port 5432

快速定位数据库服务的启动配置来源。


为什么推荐?

  1. 解决痛点:把分散在多个工具中的信息聚合起来,直接给出答案
  2. 跨平台:支持 Linux、macOS、Windows、FreeBSD
  3. 零依赖:单二进制文件,开箱即用
  4. TUI 友好:交互式界面,不熟悉命令也能用
  5. 活跃维护:14k+ Stars,持续更新

常用命令速查

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 查看进程启动链
witr <进程名>
witr --pid <PID>

# 查看端口占用
witr --port <端口号>

# 树形显示
witr --tree

# 简洁输出(只显示链路)
witr --short

# 交互模式
witr
witr -i

# 显示环境变量
witr <name> --env

# JSON 输出
witr <name> --json

总结

witr 是一个简单但强大的运维工具,它把”为什么这个进程在运行”这个常见但繁琐的问题,用一条命令就解决了。

如果你经常需要排查进程来源、端口占用问题,或者只是想更清晰地了解系统运行状态,witr 绝对值得加入你的工具箱。


相关链接