NVidia Fan Speed Revisited

One of the comments to my last post about adjusting the fan speed on your NVidia graphics card was that what was needed was a script to adjust the speed based on the temperature. The script presented here does just that.

The script takes a single argument, the desired temperature (default is 50C). It then calculates a low and high temperature based on this and increases the fan speed if the temperature gets above the max or decreases it if the temperature gets below the minimum.

It determines the temperature and fan speed by parsing the output of the nvclock command. Without further ado, here is the script:

#!/bin/bash
#
# Adjust fan speed automatically.


# Location of the nvclock program.
nvclock_bin=/usr/local/bin/nvclock

# Target temperature for video card.
target_temp=50

# Value used to calculate the temperature range (+/- target_temp).
target_range=1

# Time to wait before re-checking.
sleep_time=120

# Minimum fan speed.
min_fanspeed=20

# Fan speed increment.
adj_fanspeed=5


if [[ "$1" ]]; then target_temp=$1; fi

let target_temp_low=target_temp-target_range
let target_temp_high=target_temp+target_range


while true
do
    temp=$(echo $($nvclock_bin --info | grep -i 'GPU temperature' | cut -d ':' -f 2))
    pwm=$(echo $($nvclock_bin --info | grep -i 'PWM duty cycle' | cut -d ':' -f 2))

    temp_val=${temp/C/}
    pwm_val=${pwm%.*}

    if [[ $temp_val -gt $target_temp_high ]]; then
        # Temperature above target, see if the fan has any more juice.
        if [[ $pwm_val -lt 100 ]]; then
            echo "Increasing GPU fan speed, temperature: $temp"
            let pwm_val+=adj_fanspeed
            if [[ $pwm_val -gt 100 ]]; then pwm_val=100; fi
            $nvclock_bin -f --fanspeed $pwm_val
        fi
    elif [[ $temp_val -lt $target_temp_low ]]; then
        # Temperature below target, lower the fan speed
        # if we're not already at the minimum.
        if [[ $pwm_val -gt $min_fanspeed ]]; then
            echo "Decreasing GPU fan speed, temperature: $temp"
            let pwm_val-=adj_fanspeed
            if [[ $pwm_val -lt $min_fanspeed ]]; then pwm_val=$min_fanspeed; fi
            $nvclock_bin -f --fanspeed $pwm_val
        fi
    fi
    sleep $sleep_time
done

# vim: tabstop=4: shiftwidth=4: noexpandtab:
# kate: tab-width 4; indent-width 4; replace-tabs false;

The values set at the top of the script can be changed to adjust:

  • Where nvclock is located.
  • The default target temperature.
  • How wide the temperature range is.
  • How often the temperature is checked.
  • What the minimum fan speed is.
  • How much the fan speed is adjusted when it's changed.

Mitch Frazier is an embedded systems programmer at Emerson Electric Co. Mitch has been a contributor to and a friend of Linux Journal since the early 2000s.

Load Disqus comments