Update cpu script

This commit is contained in:
Alex Hyett 2024-04-19 16:06:45 +01:00
parent ffbd7444d8
commit 653b130ce2

View file

@ -1,53 +1,57 @@
#!/bin/bash #!/bin/bash
# Inspired by https://github.com/kmwoley/unRAID-Tools/blob/master/unraid_array_fan.sh
# User Defined variables # User Defined variables
FAN_LOW_PWM=30 FAN_LOW_PWM=30
FAN_HIGH_PWM=255 FAN_HIGH_PWM=255
FAN_START_PWM=50 # technically 30 but setting higher to ensure fan is running
LOW_TEMP=28 LOW_TEMP=28
HIGH_TEMP=60 HIGH_TEMP=60
CPU_FAN=/sys/class/hwmon/hwmon3/pwm2 CPU_FAN=/sys/class/hwmon/hwmon3/pwm2
# End of user defined variables # End of user defined variables
NUM_STEPS=$((HIGH_TEMP - LOW_TEMP))
PWM_INCREMENT=$(( (FAN_HIGH_PWM - FAN_LOW_PWM) / NUM_STEPS))
OUTPUT+="Linear PWM Range is "$FAN_LOW_PWM" to "$FAN_HIGH_PWM" in "$NUM_STEPS" increments of "$PWM_INCREMENT$'\n'
# Enable speed change on this fan if not already # Enable speed change on this fan if not already
if [ "$CPU_FAN" != "1" ]; then if [ "$CPU_FAN" != "1" ]; then
echo 1 > "${CPU_FAN}_enable" echo 1 > "${CPU_FAN}_enable"
fi fi
PREVIOUS_SPEED=`cat $CPU_FAN` i=0
CPU_TEMP=`sensors coretemp-isa-0000 | grep "CPU Temp" | grep -o '[0-9]*' | head -1` sleep_time=9
while [ $i -lt 6 ]; do # 6 ten-second intervals in 1 minute
PREVIOUS_SPEED=`cat $CPU_FAN`
CPU_TEMP=`sensors coretemp-isa-0000 | grep "CPU Temp" | grep -o '[0-9]*' | head -1`
echo "CPU Temp: "$CPU_TEMP"" echo "CPU Temp: "$CPU_TEMP""
if [ "$CPU_TEMP" -le "$LOW_TEMP" ]; then if ((CPU_TEMP <= LOW_TEMP)); then
echo $FAN_LOW_PWM > $CPU_FAN NEW_PWM=$((FAN_LOW_PWM))
OUTPUT+="Setting pwm to: "$FAN_LOW_PWM$'\n' elif ((CPU_TEMP >= HIGH_TEMP)); then
elif [ "$CPU_TEMP" -ge "$HIGH_TEMP" ]; then NEW_PWM=$((FAN_HIGH_PWM))
echo $FAN_HIGH_PWM > $CPU_FAN elif ((CPU_TEMP >= 30 && CPU_TEMP < 35)); then
OUTPUT+="Setting pwm to: "$FAN_HIGH_PWM$'\n' NEW_PWM=$((FAN_LOW_PWM + 35))
else elif ((CPU_TEMP >= 35 && CPU_TEMP < 40)); then
# set fan to starting speed first to make sure it spins up then change it to low setting. NEW_PWM=$((FAN_LOW_PWM + 35*2))
if [ "$PREVIOUS_SPEED" -lt "$FAN_START_PWM" ]; then elif ((CPU_TEMP >= 40 && CPU_TEMP < 50)); then
echo $FAN_START_PWM > $CPU_FAN NEW_PWM=$((FAN_LOW_PWM + 35*3))
sleep 4 elif ((CPU_TEMP >= 50 && CPU_TEMP < 55)); then
NEW_PWM=$((FAN_LOW_PWM + 35*4))
elif ((CPU_TEMP >= 55 && CPU_TEMP < 60)); then
NEW_PWM=$((FAN_LOW_PWM + 35*5))
else
# Assuming a failed reading
NEW_PWM=$((FAN_HIGH_PWM))
echo "Unknown temp "$CPU_TEMP""
fi fi
# Calculate target fan PWM speed as a linear value between FAN_HIGH_PWM and FAN_LOW_PWM echo $NEW_PWM > $CPU_FAN
FAN_LINEAR_PWM=$(( ((CPU_TEMP - LOW_TEMP) * PWM_INCREMENT) + FAN_LOW_PWM))
echo $FAN_LINEAR_PWM > $CPU_FAN
OUTPUT+="Setting pwm to: "$FAN_LINEAR_PWM$'\n'
fi
# produce output if the fan speed was changed # produce output if the fan speed was changed
CURRENT_SPEED=`cat $CPU_FAN` CURRENT_SPEED=`cat $CPU_FAN`
if [ "$PREVIOUS_SPEED" -ne "$CURRENT_SPEED" ]; then if [ "$PREVIOUS_SPEED" -ne "$CURRENT_SPEED" ]; then
echo "Fan speed has changed to PWM: "$CURRENT_SPEED" CPU temp: "$CPU_TEMP"" echo "Speed Changed. PWM: "$PREVIOUS_SPEED" -> "$CURRENT_SPEED" CPU Temp: "$CPU_TEMP""
echo "${OUTPUT}" else
else echo "No Speed Change. PWM: "$CURRENT_SPEED" CPU Temp: "$CPU_TEMP" "
echo "Fan speed unchanged. CPU temp: "$CPU_TEMP" PWM: "$CURRENT_SPEED"" fi
fi
echo "Sleeping for "$sleep_time" seconds"
sleep $sleep_time
i=$(( i + 1 ))
done