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?

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix