在 CentOS 上让 cpustat(通常指 sysstat 包里的 mpstat/sar,或 cpustat 工具)后台运行,常见有几种方式,按使用场景给你列一下:
nohup(最常用)适合:临时后台跑,退出终端也不中断
nohup cpustat 1 > cpustat.log 2>&1 &
说明:
1:每 1 秒采样一次> cpustat.log:输出到文件2>&1:错误也写进去&:后台运行查看:
tail -f cpustat.log
结束:
ps aux | grep cpustat
kill <pid>
screen / tmux(推荐长期用)适合:你还要随时回来看
screen -S cpu
cpustat 1
退出但保持运行:
Ctrl + A 然后按 D
重新进入:
screen -r cpu
systemd(服务器长期监控)适合:正式服务
创建服务文件:
vim /etc/systemd/system/cpustat.service
示例:
[Unit]
Description=cpustat monitor
[Service]
ExecStart=/usr/bin/cpustat 1
Restart=always
[Install]
WantedBy=multi-user.target
启用:
systemctl daemon-reload
systemctl start cpustat
systemctl enable cpustat
查看:
journalctl -u cpustat -f
mpstat(sysstat)很多人说的“cpustat”其实是:
nohup mpstat 1 > mpstat.log 2>&1 &
nohupscreensystemd如果你说的是 某个具体 cpustat 命令或版本,把命令贴出来我可以直接帮你写后台命令。