linux - 如何从bash获得平均CPU温度?
问题描述
如何在 Linux 上从 bash 获取平均 CPU 温度?最好以华氏度为单位。该脚本应该能够处理不同数量的 CPU。
解决方案
你这样做:
安装
sudo apt install lm-sensors
sudo sensors-detect --auto
get_cpu_temp.sh
#!/bin/bash
# 1. get temperature
## a. split response
## Core 0: +143.6°F (high = +186.8°F, crit = +212.0°F)
IFS=')' read -ra core_temp_arr <<< $(sensors -f | grep '^Core\s[[:digit:]]\+:') #echo "${core_temp_arr[0]}"
## b. find cpu usage
total_cpu_temp=0
index=0
for i in "${core_temp_arr[@]}"; do :
temp=$(echo $i | sed -n 's/°F.*//; s/.*[+-]//; p; q')
let index++
total_cpu_temp=$(echo "$total_cpu_temp + $temp" | bc)
done
avg_cpu_temp=$(echo "scale=2; $total_cpu_temp / $index" | bc)
## c. build entry
temp_status="CPU: $avg_cpu_temp F"
echo $temp_status
exit 0
输出
中央处理器:135.50 F
推荐阅读
- android - Kotlin Android Extensions 替换 findViewById 如何防止空视图?
- android - 如何在对象 Gson/Retrofit2 中获取字符串
- python - 确定性转换在 pymc3 模块中到底做了什么
- php - 为什么 Laravel-json-api 更新不起作用?
- mysql - SQL 数据模型:我应该如何存储对象(一种类型),其中一些将在所有用户之间共享,一些仅分配给特定用户?
- sql - 选择 sql 查询的联合部分
- java - 按数字顺序对数字的 ArrayList 进行排序
- spring - 如何在 Spring JSON 查询中不发送 @IdClass 对象
- react-native - eslint prettier 需要文件重排
- reactjs - 用本地存储反应路由器私有路由?