在 CentOS 上如果你想定时使用 cpustat 收集 CPU 使用情况,常见有几种方式,下面按使用场景给你说明。
cpustat 是否安装cpustat 通常来自 sysstat 包:
which cpustat
如果没有:
yum install sysstat -y
CentOS 7/8 通用
cron 定时执行(最常用)crontab -e
*/5 * * * * /usr/bin/cpustat >> /var/log/cpustat.log 2>&1
cron 不支持秒级,可用 sleep 变通:
* * * * * for i in 1 2 3 4 5 6; do /usr/bin/cpustat >> /var/log/cpustat.log; sleep 10; done
watch 临时实时监控(非记录)适合临时看:
watch -n 5 cpustat
每 5 秒刷新一次(不写文件)
systemd timer(CentOS 7+ 推荐)vi /etc/systemd/system/cpustat.service
[Unit]
Description=cpustat logging
[Service]
ExecStart=/usr/bin/cpustat >> /var/log/cpustat.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 cpustat.timer
systemctl start cpustat.timer
cpustat 1 5
1:间隔 1 秒5:采样 5 次tail -f /var/log/cpustat.log
如果你能告诉我:
我可以给你更精确的配置。