温馨提示×

centos cpustat怎么定时

小樊
46
2026-09-09 05:56:43
栏目: 智能运维

在 CentOS 上定时使用 cpustat(或 mpstat / sar 等 CPU 统计工具),通常有两种常见做法:


一、先用哪个命令?

  • cpustat:一般来自 sysstat 或第三方工具(如 cpustat by sysstat)
  • 常用替代:mpstat(sysstat 自带,更常见)
# 查看是否安装
which mpstat
which cpustat

# 没装就装
yum install sysstat -y

二、方式一:直接用命令本身定时(最简单)

1️⃣ mpstat 每 5 秒采样一次,共 12 次(1 分钟)

mpstat 5 12

2️⃣ cpustat(如果是指 sysstat 的)

cpustat 5 12

三、方式二:用 cron 定时执行(推荐)

1️⃣ 编辑定时任务

crontab -e

2️⃣ 示例:每 5 分钟记录一次 CPU 状态

*/5 * * * * /usr/bin/mpstat 1 1 >> /var/log/cpu_stat.log 2>&1

或(如果真是 cpustat):

*/5 * * * * /usr/bin/cpustat 1 1 >> /var/log/cpu_stat.log 2>&1

3️⃣ 查看日志

tail -f /var/log/cpu_stat.log

四、方式三:用 systemd timer(CentOS 7+)

1️⃣ 创建 service

vi /etc/systemd/system/cpustat.service
[Unit]
Description=CPU stat logger

[Service]
ExecStart=/usr/bin/mpstat 1 1 >> /var/log/cpu_stat.log

2️⃣ 创建 timer

vi /etc/systemd/system/cpustat.timer
[Unit]
Description=Run cpustat every 5 minutes

[Timer]
OnCalendar=*:0/5
Persistent=true

[Install]
WantedBy=timers.target

3️⃣ 启动

systemctl daemon-reload
systemctl enable --now cpustat.timer

五、常见组合推荐 ✅

需求 推荐
临时看 mpstat 1
长期记录 cron + 日志
服务器级监控 sar
图形化 node_exporter + Prometheus

如果你能告诉我:

  • 用的是 CentOS 几
  • cpustat 还是 mpstat
  • 保存多久 / 多久采一次

我可以直接帮你写完整配置。

0