在 CentOS 里,cpustat 通常是 sysstat 工具包里的命令(也有同名其他工具,但最常见的是 sysstat 的 mpstat/sar 类工具;如果你指的是 cpustat 这个命令,一般用法类似)。
把输出写到文件,本质就是 重定向或让命令自己支持日志。
下面分几种常见情况:
cpustat > cpu.log
或追加:
cpustat >> cpu.log
如果 cpustat 需要参数(例如间隔和次数):
cpustat 1 10 > cpu.log
表示每 1 秒采样一次,共 10 次。
cpustat 1 > cpu.log 2>&1 &
2>&1:把错误输出也写进文件&:后台运行查看进程:
jobs
ps aux | grep cpustat
停止:
kill %1
nohup(断开终端也运行)nohup cpustat 1 > cpu.log 2>&1 &
mpstat很多人把 mpstat 当成 cpu 统计工具:
mpstat 1 10 > cpu.log
或记录到 sysstat 日志(自动):
sar -u 1 10 > cpu.log
* * * * * /usr/bin/cpustat 1 60 >> /var/log/cpu.log
每分钟采集 60 秒。
如果你说的 不是 sysstat 的 cpustat,而是某个特定工具(比如 github.com/uber-common/cpustat),告诉我一下,我可以给你精确命令。