Skip to content

Instantly share code, notes, and snippets.

@bygreencn
Forked from squadbox/cool_gpu2.sh
Last active May 5, 2019 07:33
Show Gist options
  • Save bygreencn/bf31ff33d1dd03cc0303d474f24a2c6d to your computer and use it in GitHub Desktop.
Save bygreencn/bf31ff33d1dd03cc0303d474f24a2c6d to your computer and use it in GitHub Desktop.
A script to control Nvidia GPU fan speed on headless (non-X) linux nodes
#!/bin/bash
# cool_gpu2.sh This script will enable or disable fixed gpu fan speed
#
# Description: A script to control GPU fan speed on headless (non-X) linux nodes
# Original Script by Axel Kohlmeyer <akohlmey@gmail.com>
# https://sites.google.com/site/akohlmey/random-hacks/nvidia-gpu-coolness
#
# Modified for newer drivers and removed old work-arounds
# Tested on Ubuntu 16.04 with driver 418.39
# Copyright 2019, bygreencn
# Copyright 2015, squadbox
# Requirements:
# * An Nvidia GPU
# * Nvidia Driver V285 or later
# * xorg
# * Coolbits enabled and empty config setting
# nvidia-xconfig -a --cool-bits=28 --allow-empty-initial-configuration
# You may have to run this as root or with sudo if the current user is not authorized to start X sessions.
# Paths to the utilities we will need
SMI='/usr/bin/nvidia-smi'
SET='/usr/bin/nvidia-settings'
# Determine major driver version
VER=`awk '/NVIDIA/ {print $8}' /proc/driver/nvidia/version | cut -d . -f 1`
# Drivers from 285.x.y on allow persistence mode setting
if [ ${VER} -lt 285 ]
then
echo "Error: Current driver version is ${VER}. Driver version must be greater than 285."; exit 1;
fi
# Read a numerical command line arg between 40 and 100
if [ "$1" -eq "$1" ] 2>/dev/null && [ "0$1" -ge "40" ] && [ "0$1" -le "100" ]
then
eval "$SMI -pm 1" >> /dev/null # enable persistance mode
speed=$1 # set speed
echo "Setting fan to $speed%."
# how many GPU's are in the system?
NUMGPU="$(nvidia-smi -L | wc -l)"
# loop through each GPU and individually enable fan control
n=0
while [ $n -lt $NUMGPU ];
do
# start an x session, and call nvidia-settings to enable fan control
eval "DISPLAY=:0 XAUTHORITY=/var/run/lightdm/root/:0 nvidia-settings -a [gpu:${n}]/GPUFanControlState=1" >> /dev/null
let n=n+1
done
# how many GPU's fans are in the system?
NUMFAN="$(DISPLAY=:0 XAUTHORITY=/var/run/lightdm/root/:0 nvidia-settings -q fans | grep "\[fan" | wc -l)"
# loop through each FAN and individually set fan speed
n=0
while [ $n -lt $NUMFAN ];
do
# start an x session, and call nvidia-settings to set speed
eval "DISPLAY=:0 XAUTHORITY=/var/run/lightdm/root/:0 nvidia-settings -a [fan:${n}]/GPUTargetFanSpeed=$speed" >> /dev/null
let n=n+1
done
echo "Complete"; exit 0;
elif [ "$1" = "normal" ]
then
eval "$SMI -pm 0" >> /dev/null # disable persistance mode
echo "Enabling default auto fan control."
# how many GPU's are in the system?
NUMGPU="$(nvidia-smi -L | wc -l)"
# loop through each GPU and individually set fan speed
n=0
while [ $n -lt $NUMGPU ];
do
# start an x session, and call nvidia-settings to enable fan control and set speed
eval "DISPLAY=:0 XAUTHORITY=/var/run/lightdm/root/:0 nvidia-settings -a [gpu:${n}]/GPUFanControlState=0" >> /dev/null
let n=n+1
done
echo "Complete"; exit 0;
else
echo "Error: Please pick a fan speed between 40 and 100, or normal."; exit 1;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment