温馨提示×

centos cpustat怎么后台跑

小樊
43
2026-10-03 06:19:28
栏目: 智能运维

在 CentOS 上让 cpustat(通常指 sysstat 包里的 mpstat/sar,或 cpustat 工具)后台运行,常见有几种方式,按使用场景给你列一下:


1️⃣ 用 nohup(最常用)

适合:临时后台跑,退出终端也不中断

nohup cpustat 1 > cpustat.log 2>&1 &

说明:

  • 1:每 1 秒采样一次
  • > cpustat.log:输出到文件
  • 2>&1:错误也写进去
  • &:后台运行

查看:

tail -f cpustat.log

结束:

ps aux | grep cpustat
kill <pid>

2️⃣ 用 screen / tmux(推荐长期用)

适合:你还要随时回来看

screen -S cpu
cpustat 1

退出但保持运行:

Ctrl + A 然后按 D

重新进入:

screen -r cpu

3️⃣ 用 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

4️⃣ 如果只是想用 mpstat(sysstat)

很多人说的“cpustat”其实是:

nohup mpstat 1 > mpstat.log 2>&1 &

✅ 建议

  • 临时看 → nohup
  • 经常看 → screen
  • 生产环境 → systemd

如果你说的是 某个具体 cpustat 命令或版本,把命令贴出来我可以直接帮你写后台命令。

0 踩