如何从bash获得平均CPU温度?

时间:2018-06-04 17:56:36

标签: linux bash cpu temperature

如何在Linux上获得bash的平均CPU温度?最好以华氏度为单位。该脚本应该能够处理不同数量的CPU。

2 个答案:

答案 0 :(得分:1)

您还可以直接从sysfs读取CPU温度(尽管机器/操作系统之间的路径可能有所不同):

重击:

temp_file=$(mktemp -t "temp-"$(date +'%Y%m%d@%H:%M:%S')"-XXXXXX")
ls $temp_file
while true; do
    cat /sys/class/thermal/thermal_zone*/temp | tr '\n' ' ' >> "$temp_file"
    printf "\n" >> $temp_file
    sleep 2
done

如果您是鱼类用户,则可以在配置目录中添加一个函数,例如:~/.config/fish/functions/temp.fish

function temp
    set temp_file (mktemp -t "temp-"(date +'%Y%m%d@%H:%M:%S')"-XXXXXX")
    ls $temp_file
    while true
        cat /sys/class/thermal/thermal_zone*/temp | tr '\n' ' ' >> "$temp_file"
        printf "\n" >> $temp_file
        sleep 2
    end
end

示例

enter image description here

答案 1 :(得分:0)

你是这样做的:

<强>安装

ap-get install lm-sensors
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

<强>输出

CPU:135.50 F

相关问题