在 Debian 上如果你想定时使用 cpustat(或定期采集 CPU 统计),常见做法有几种,下面按推荐程度给你说明。
sudo apt update
sudo apt install cpustat
cpustat 1 5
表示:每 1 秒采样一次,共 5 次
编辑当前用户的 cron:
crontab -e
*/5 * * * * /usr/bin/cpustat 1 1 >> /var/log/cpustat.log 2>&1
0 0 * * * /usr/bin/cpustat 1 10 >> /var/log/cpustat.log 2>&1
⚠ 注意
- cron 环境 PATH 可能不完整,建议写绝对路径
- 日志文件要有写权限
sudo nano /etc/systemd/system/cpustat.service
内容:
[Unit]
Description=Run cpustat
[Service]
ExecStart=/usr/bin/cpustat 1 5
sudo nano /etc/systemd/system/cpustat.timer
内容:
[Unit]
Description=Timer for cpustat
[Timer]
OnCalendar=*:0/5
Persistent=true
[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable --now cpustat.timer
nohup cpustat 5 > /var/log/cpustat.log &
适合短期调试,不适合生产。
sar(sysstat)top -b -d 5 >> logmpstat如果你能告诉我:
我可以帮你写一套完整配置。