NVidia Fan Speed Revisited

 in

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 Associate Editor for Linux Journal.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Update script

DarkPhoinix's picture
#!/bin/bash
#
# Adjust fan speed automatically.
# Ver. DarkPhoinix


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

# Target temperature for video card.
target_temp=50

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

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

# Minimum fan speed.
min_fanspeed=20

# Fan speed increment.
adj_fanspeed=1

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 'Fanspeed' | 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;

Something in /etc/init.d

Mitch Frazier's picture

I inserted it a line to execute it into /etc/init.d/boot.local, but that's on openSuSE so it may have to go somewhere else in your case depending on what distro you use.

And actually I just inserted a line to run nvclock and set the speed to 40% on boot:

...
# /etc/init.d/boot.local
...
nvclock_bin=/home/mitch/bindirs/nvclock/bin/nvclock
nvclock_speed=40

if [[ -x $nvclock_bin ]]; then
        $nvclock_bin -f -F $nvclock_speed
fi

Mitch Frazier is an Associate Editor for Linux Journal.

how do i run this script?

Anonymous's picture

What program package do i need to insert this into to run this script?

Webcast
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers

Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.

Learn More

Sponsored by AMD

White Paper
Red Hat White Paper: Using an Open Source Framework to Catch the Bad Guy

Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6

Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.

Learn more about catching the bad guy in this free white paper.

Learn More

Sponsored by DLT Solutions