cpustat 是 sysstat 工具集里的一个性能统计工具,用来查看 CPU 使用情况。下面分 安装、基本用法、常用参数、设置采样间隔与次数、定时采集、保存日志 等方面说明(以 CentOS 7 / 8 / Rocky / Alma 通用)。
yum install -y sysstat
CentOS 8 / Stream:
dnf install -y sysstat
安装完成后确认:
which cpustat
cpustat -V
cpustat
输出示例:
CPU usr sys nice idle iowait irq softirq steal guest guest_nice
all 12 8 0 78 2 0 0 0 0 0
cpustat [间隔秒] [次数]
✅ 每 2 秒 采样一次,共 5 次:
cpustat 2 5
✅ 每 1 秒 持续采样(直到 Ctrl+C):
cpustat 1
cpustat -c 0,1
查看 CPU0 和 CPU1:
cpustat -c 0,1 1 5
一般不用额外设置,但可明确:
cpustat -p
cpustat -i
cpustat -t
nohup cpustat 1 > /var/log/cpustat.log 2>&1 &
tail -f /var/log/cpustat.log
ps -ef | grep cpustat
kill PID
crontab -e
每 5 分钟采样一次:
*/5 * * * * /usr/bin/cpustat 1 1 >> /var/log/cpustat.log
| 字段 | 含义 |
|---|---|
| usr | 用户态 CPU |
| sys | 内核态 CPU |
| idle | 空闲 |
| iowait | IO 等待 |
| irq | 硬件中断 |
| softirq | 软中断 |
| steal | 虚拟化被抢占 |
top:交互式、进程级cpustat:CPU 级、适合性能统计和日志确认是否安装 sysstat:
rpm -qa | grep sysstat
如果你是想 监控某个进程 CPU、设置成系统服务、或 和 Prometheus / Grafana 对接,可以直接告诉我你的使用场景,我可以给你更精确的配置方案。