自动化与脚本
追踪进程/服务/端口为何运行:用 witr 查看因果链
当你不清楚某个进程、服务或端口为什么在运行、由谁启动时,使用 witr 快速查看其因果链。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 38/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 60/100 ×0.15
适用场景
追踪进程/服务/端口为何运行:用 wit特定
解决思路
- 安装 witr(Unix):运行 `curl -fsSL https://raw.githubusercontent.com/pranshuparmar/witr/main/install.sh | bash`
- 安装 witr(Windows PowerShell):运行 `irm https://raw.githubusercontent.com/pranshuparmar/witr/main/install.ps1 | iex`
- 用包管理器安装 witr:Homebrew 用 `brew install witr`;Conda 用 `conda install -c conda-forge witr`;Arch Linux 用 `yay -S witr-bin`;Windows winget 用 `winget install -e --id PranshuParmar.witr`;Windows Scoop 用 `scoop install main/witr`;Alpine 用 `sudo apk add --allow-untrusted ./witr-*.apk`;从 Go 源码安装用 `go install github.com/pranshuparmar/witr/cmd/witr@latest`
- 按 PID 检查进程:运行 `witr <pid>`
- 按进程名检查(子串匹配):运行 `witr <name>`
- 检查端口占用:运行 `witr --port <port>` 或 `witr -p <port>`
- 启动交互式 TUI 仪表盘:运行 `witr --interactive` 或 `witr -i`
- 显示所有运行中进程及其因果信息:运行 `witr --all` 或 `witr -a`
- 输出 JSON 以便脚本处理:运行 `witr --json <pid>`
- 跟随/监视进程并自动刷新:运行 `witr --watch <pid>` 或 `witr -w <pid>`
- 输出详细信息(完整环境和元数据):运行 `witr --verbose <pid>` 或 `witr -v <pid>`
- 按用户过滤进程:运行 `witr --user <username>`
- 显示版本:运行 `witr --version`
- TUI 中导航进程列表:使用 `↑` / `↓` 方向键
- TUI 中展开进程详情/因果链:按 `Enter`
- TUI 中过滤/搜索进程:按 `f`
- TUI 中按列排序:按 `s`
- TUI 中刷新:按 `r`
- TUI 中切换 JSON 视图:按 `j`
- TUI 中退出:按 `q` 或 `Ctrl+C`
- 查找谁启动了一个端口监听:运行 `witr --port 5432`;获取机器可读输出用于自动化:运行 `witr --json --port 5432 | jq '.causality_chain[-1].service'`
- 审计所有运行中服务:运行 `witr --all | less`;导出完整审计到 JSON:运行 `witr --all --json > audit.json`
- 检查 Docker/容器进程:运行 `witr <pid-of-containerized-process>`,输出会显示容器 → 容器运行时 → systemd 链
- 在 shell 脚本中使用:运行 `if witr --json --port 8080 > /tmp/witr_out.json 2>/dev/null; then SERVICE=$(jq -r '.causality_chain[-1].service // "unknown"' /tmp/witr_out.json); echo "Port 8080 is owned by service: $SERVICE"; else echo "Port 8080 is not in use"; fi`
- 按用户过滤进程(示例):运行 `witr --all --user www-data`
- 在 Go 项目中嵌入 witr 逻辑:运行 `go get github.com/pranshuparmar/witr`;然后使用 `inspector.InspectPID(1234)` 检查进程;使用 `inspector.InspectPort(8080, "tcp")` 检查端口
- 处理权限拒绝:运行 `sudo witr <pid>` 或 `sudo witr --port 80`
- 安装后二进制文件未找到:运行 `export PATH="$PATH:/usr/local/bin"` 或 `export PATH="$PATH:$(go env GOPATH)/bin"`
- 端口未找到/无输出:先用 `ss -tlnp | grep <port>`(Linux)或 `netstat -an | grep <port>`(macOS/Windows)确认端口在监听,然后重试 `witr --port <port>`
- TUI 渲染不正确:运行 `export TERM=xterm-256color` 然后 `witr --interactive`;在 Windows 上使用 Windows Terminal
- 进程在检查前退出:只能检查当前运行的进程;对长运行进程使用 `--watch` 监控:运行 `witr --watch <pid>`
- macOS 某些进程需要提升权限:运行 `sudo witr <pid>`
适用边界 · 注意事项
- 只能检查当前正在运行的进程;进程已退出时无法回溯其历史,除非事先用 --watch 监控。
- 在无法安装 witr 或没有权限读取 /proc(Linux)等系统信息的环境中不适用,部分系统进程需要 sudo。
- 不适合需要跨主机或长期历史审计的场景,witr 只反映当前本机状态。