在 CentOS 上 cpustat 掉线/中断/不再输出,通常不是“cpustat 本身坏了”,而是运行环境、采样方式或系统状态导致的。下面按最常见原因 → 排查方法 → 解决办法给你一个系统性的说明。
请先对号入座:
不同情况原因不同。
sysstat 里的 mpstat / sar,而不是 cpustat?很多人把
mpstat当成cpustat
CentOS 默认 没有 cpustat 命令,常见来源:
| 工具 | 来源 |
|---|---|
mpstat |
sysstat |
sar |
sysstat |
cpustat |
第三方工具(如 cpustat by Thomas Habets) |
✅ 检查:
which cpustat
rpm -qf $(which cpustat)
如果你是:
ssh user@centos
cpustat
SSH 断开 = 进程收到 SIGHUP = 退出
✅ 解决方式:
screenyum install -y screen
screen
cpustat
# Ctrl+A D 离开
tmuxyum install -y tmux
tmux
cpustat
nohupnohup cpustat > cpu.log 2>&1 &
系统内存不足时 OOM Killer 会直接杀进程。
✅ 检查:
dmesg | egrep -i "oom|kill"
journalctl -k | grep -i oom
✅ 表现:
✅ 解决:
某些 cpustat 实现是:
sleep(interval)
如果系统 负载极高 / IO 卡死 / NUMA 抖动,可能表现为:
✅ 排查:
top
vmstat 1
iostat -x 1
cpustat 本质读的是:
/proc/stat
✅ 测试:
watch -n1 "cat /proc/stat | head"
如果这里都卡,说明不是 cpustat 问题,而是:
例如:
github.com/pehringer/cpustat
常见问题:
| 原因 | 说明 |
|---|---|
| Go 程序被 kill | OOM |
没加 -i |
默认只跑一次 |
| 终端关闭 | 进程退出 |
| 权限不足 | 某些 cgroup 读不到 |
✅ 建议用法:
nohup ./cpustat -i 1 > cpu.log 2>&1 &
# 1. 是否还在运行
ps -ef | grep cpustat
# 2. 是否被 kill
dmesg | tail -50
# 3. 是否 SSH 导致
tmux
# 4. 是否是 mpstat
mpstat 1
# 5. 系统是否卡
top
你可以直接把下面信息贴出来,我可以精确判断:
cat /etc/centos-release
uname -r
which cpustat
cpustat --help
掉线时的现象(截图或文字)
是否云服务器 / 虚拟机 / 物理机
CentOS 上 cpustat “掉线”,90% 是 SSH 断开或被 OOM 杀掉,不是 cpustat 本身的问题。
如果你告诉我你用的是 哪个 cpustat,我可以给你最合适的使用方式。