> 本文作者:丁辉 # 检测CPU负载 ## 介绍 CPU利用率可以用以下公式计算: CPU利用率(%) = 1 - (闲置时间 / 总时间) x 100 **解释** - **闲置时间**:在指定的时间段内,CPU处于闲置状态的时间总和。 - **总时间**:这个时间段的总时长,通常包括CPU的工作时间和闲置时间。 ## 脚本检测 - 使用 Sysstat 工具检测 CPU 负载 1. 安装 Sysstat - Centos ```bash yum -y install sysstat ``` - Ubuntu ```bash apt-get -y install sysstat ``` 2. 执行脚本 ```bash curl -sfL https://gitee.com/offends/Linux/raw/main/File/Shell/check-cpu-sysstat.sh | bash ``` - 根据 `/proc/stat` 数据计算 CPU 负载 ```bash curl -sfL https://gitee.com/offends/Linux/raw/main/File/Shell/check-cpu.sh | bash ```