在 CentOS 上定时使用 cpustat(或 mpstat / sar 等 CPU 统计工具),通常有两种常见做法:
sysstat 或第三方工具(如 cpustat by sysstat)# 查看是否安装
which mpstat
which cpustat
# 没装就装
yum install sysstat -y
mpstat 5 12
cpustat 5 12
crontab -e
*/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
tail -f /var/log/cpu_stat.log
vi /etc/systemd/system/cpustat.service
[Unit]
Description=CPU stat logger
[Service]
ExecStart=/usr/bin/mpstat 1 1 >> /var/log/cpu_stat.log
vi /etc/systemd/system/cpustat.timer
[Unit]
Description=Run cpustat every 5 minutes
[Timer]
OnCalendar=*:0/5
Persistent=true
[Install]
WantedBy=timers.target
systemctl daemon-reload
systemctl enable --now cpustat.timer
| 需求 | 推荐 |
|---|---|
| 临时看 | mpstat 1 |
| 长期记录 | cron + 日志 |
| 服务器级监控 | sar |
| 图形化 | node_exporter + Prometheus |
如果你能告诉我:
我可以直接帮你写完整配置。