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?

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState