温馨提示×

ubuntu cpustat能否后台

小樊
45
2026-10-12 16:45:30
栏目: 智能运维

可以的,cpustat(通常指 sysstat 包里的 mpstat/pidstat,或 GitHub 上的 cpustat 工具)完全可以在后台运行,常见做法有几种:


1. 用 nohup 后台运行(最常用)

nohup cpustat -i 1 > cpustat.log 2>&1 &
  • &:放入后台
  • nohup:断开 SSH 也不会停
  • 输出写入 cpustat.log

查看:

tail -f cpustat.log

2. 用 screen / tmux(推荐)

screen -S cpu
cpustat -i 1
  • Ctrl + A + D detach
  • 随时 screen -r cpu 回来

适合长时间监控。


3. 作为系统服务(server 场景)

创建 systemd 服务:

# /etc/systemd/system/cpustat.service
[Unit]
Description=cpustat monitor

[Service]
ExecStart=/usr/bin/cpustat -i 5
Restart=always

[Install]
WantedBy=multi-user.target

启用:

systemctl enable --now cpustat

4. 定时采样(cron)

如果是 sysstat 的 mpstat:

*/5 * * * * mpstat 1 1 >> /var/log/mpstat.log

小提示

  • 明确你说的 cpustat 是哪个:
    • sysstat 的 mpstat
    • Go 写的 cpustat(erikdubois / tools)
  • 后台跑注意 日志轮转,避免磁盘写满

如果你告诉我你用的是哪种 cpustat,我可以给你更精确的命令。

0 踩