Skip to content

Instantly share code, notes, and snippets.

@nick-shmyrev
Last active May 17, 2021 03:17
Show Gist options
  • Select an option

  • Save nick-shmyrev/1388ad02b83e9e4faac6d72814a24e23 to your computer and use it in GitHub Desktop.

Select an option

Save nick-shmyrev/1388ad02b83e9e4faac6d72814a24e23 to your computer and use it in GitHub Desktop.
nvidia fan control bash script
#!/bin/bash
# Settings
readonly MIN_SPEED=27
readonly MAX_SPEED=100
readonly HIGH_TEMP=87
readonly NORMAL_TEMP=35
init() {
nvidia-settings -a "GPUFanControlState=1"
}
get_gpu_temp() {
echo `nvidia-settings -q GPUCoreTemp |awk -F ":" 'NR==2{print $3}' |sed 's/[^0-9]*//g'`
}
get_fan_speed() {
local current_temp=$1
local new_fan_speed=$(( $MIN_SPEED + (($MAX_SPEED - $MIN_SPEED) / ($HIGH_TEMP - $NORMAL_TEMP) * ($current_temp - $NORMAL_TEMP)) ))
if (($new_fan_speed > $MIN_SPEED && $new_fan_speed < $MAX_SPEED));
then
echo $new_fan_speed
elif (($new_fan_speed > $MAX_SPEED))
then
echo $MAX_SPEED
else
echo $MIN_SPEED
fi
}
set_fan_speed() {
local new_fan_speed=$1
# echo "New fan speed: ${new_fan_speed}"
nvidia-settings -a "GPUTargetFanSpeed=${new_fan_speed}" 2>&1 >/dev/null
}
# Run script
init
while true
do
current_temp=$(get_gpu_temp)
new_fan_speed=$(get_fan_speed $current_temp)
set_fan_speed $new_fan_speed
sleep 2s
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment