[cron] シェルを自動実行してLinuxのGPUのファンをCurve Controlする

# 環境
Linux Ubuntu bionic xfce


# はじめに
LinuxでNvidiaのグラボをカーブコントロール(グラボの温度に応じてファンを制御すること)をするためにWebを漁りなんとか解決にたどり着いた。

# 手順

1. ファンコントロールをするシェルを用意する　(https://github.com/FedoraTipper/Nvidia-Fan-Curve-Linux)
2. crontabで起動時に自動実行するようにする



# 1.ファン制御するシェル

```bash:/path/to/script
#!/bin/bash
#Make sure to enable nvidia-xconfig --cool-bits=4


#This new script works with version 378.13 driver
#Old script is not functional (on Arch, at least)
#Changed up some options and flags for nvidia-settings
#Added some comments for clarity, in case other people want to make improved forks

#enable headless mode 
#request lightdm installed - apt install --no-install-recommends xorg lightdm for debian/ubuntu
headless=true
verbose=false

if [ "$headless" = true ] ; then
    export DISPLAY=:0 XAUTHORITY=/var/run/lightdm/root/:0
fi

#Enable user defined fancontrol for all gpu
nvidia-settings -a "GPUFanControlState=1"

while true
do

    #gpu index
    i=0

    #Get GPU temperature of all cards
    for gputemp in $(nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader);do
    
    if [ "$verbose" = true ] ; then
        echo "gpu ${i} temp ${gputemp}"; 
    fi

        #Note: you need to set the minimum fan speed to a non-zero value, or it won't work
        #This fan profile is being used in my GTX580 (Fermi). Change it as necessary

        #If temperature is between X to Y degrees, set fanspeed to Z value
        case "${gputemp}" in
                0[0-9])
                        newfanspeed="10"
                        ;;
                1[0-9])
                        newfanspeed="15"
                        ;;
                2[0-9])
                        newfanspeed="20"
                        ;;
                3[0-9])
                        newfanspeed="20"
                        ;;
                4[0-9])
                        newfanspeed="30"
                        ;;
                5[0-4])
                        newfanspeed="35"
                        ;;
                5[5-6])
                        newfanspeed="40"
                        ;;
                5[7-9])
                        newfanspeed="50"
                        ;;
                6[0-5])
                        newfanspeed="60"
                        ;;
                6[6-9])
                        newfanspeed="65"
                        ;;
                7[0-5])
                        newfanspeed="70"
                        ;;
                7[6-9])
                        newfanspeed="85"
                        ;;
                *)
                        newfanspeed="100"
                        ;;
        esac
        
        nvidia-settings -a "[fan-${i}]/GPUTargetFanSpeed=${newfanspeed}" 2>&1 >/dev/null
        
        if [ "$verbose" = true ] ; then
            echo "gpu ${i} new fanspeed ${newfanspeed}"; 
        fi
        
        sleep 1s
    #increment gpu index
    i=$(($i+1))
    done
done
```


# 2. crontab設定

## 設定

### エディタで追記
```:エディタ起動
crontab -e
```
```:/tmp/crontab.xxxxx
@reboot /path/to/script
```
### ワンライナーで追記
```
echo '@reboot /path/to/script' | crontab
```
### 設定内容を確認
```
crontab -l
```
```:結果
@reboot /path/to/script
```