可以的。cpustat(比如 sysstat 包里的 mpstat 常被误称为 cpustat,也可能是 cpustat 工具本身)完全可以后台运行,常见有几种方式。
nohup 后台运行(最常用)nohup cpustat -i 1 > cpustat.log 2>&1 &
或如果是 mpstat:
nohup mpstat 1 > mpustat.log 2>&1 &
1:每 1 秒采样一次&:后台运行nohup:断开终端也不退出cpustat.log查看:
tail -f cpustat.log
screen / tmux(推荐)screen -S cpu
cpustat -i 1
然后按 Ctrl+A D detach。
重新进入:
screen -r cpu
如果你希望 开机自启、长期后台:
# /etc/systemd/system/cpustat.service
[Unit]
Description=CPU stat logging
[Service]
ExecStart=/usr/bin/mpstat 1
StandardOutput=append:/var/log/cpustat.log
Restart=always
[Install]
WantedBy=multi-user.target
启用:
systemctl daemon-reload
systemctl enable --now cpustat
如果只是想定期记录:
*/5 * * * * mpstat 1 5 >> /var/log/cpustat.log
cpustat 会 持续占用 CPU 和 I/O(尤其 1 秒级)logrotate如果你说的是 具体某个 cpustat 工具(比如 linux-tools 里的 cpustat),可以告诉我,我可以给你更精确的命令。