温馨提示×

ubuntu cpustat能后台跑吗

小樊
39
2026-10-24 11:03:50
栏目: 智能运维

可以的。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

三、作为系统服务(长期后台监控)

如果你希望 开机自启、长期后台:

systemd 示例(mpstat)

# /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
  • 如果只是排查问题,不建议长期 1 秒级后台跑

如果你说的是 具体某个 cpustat 工具(比如 linux-tools 里的 cpustat),可以告诉我,我可以给你更精确的命令。

0 踩