查看完整版本 : Fancontrol according to hard disk temperature 使 Linux 根據硬碟溫度控制風扇轉速

matthew_wai 2019-2-28 06:11 PM

Fancontrol according to hard disk temperature 使 Linux 根據硬碟溫度控制風扇轉速

Thanks to “路寅甲”, I got a simple script to control fan speed according to hard drive temperature via [b][color=Blue][url=https://pkgs.org/download/fancontrol]Fancontrol[/url][/color][/b], [b][color=Blue][url=https://pkgs.org/download/hddtemp]Hddtemp[/url][/color][/b], and [b][color=Blue][url=https://www.archlinux.org/packages/?name=lm_sensors]Lm-sensors[/url][/color][/b]. In the following script, “[b][color=Red]/dev/sda[/color][/b]” is the hard disk to be monitored, and “[b][color=Red]/Fancontrol/Hddtemp[/color][/b]” is the output file to be read by Fancontrol. Press [b][color=Red]Ctrl[/color] + [color=Red]Alt[/color] + [color=Red]T[/color][/b] to open Terminal and run the following command to check whether “[b][color=Red]/dev/sda[/color][/b]” is the correct one:[code]sudo hddtemp /dev/sd[a-z][/code]Use only the one supported by Hddtemp, which will display the temperature rather than “S.M.A.R.T. not available”.
Replace “[b][color=Red]/dev/sda[/color][/b]” with the correct one in the script if necessary.
If you have not yet configured Fancontrol, see [b][color=Blue][url=https://wiki.archlinux.org/index.php/fan_speed_control]this page[/url][/color][/b], [b][color=Blue][url=https://askubuntu.com/questions/22108/]this page[/url][/color][/b], and [b][color=Blue][url=http://tuxtweaks.com/2008/08/how-to-control-fan-speeds-in-ubuntu/]this page[/url][/color][/b] and run the following commands one by one (restart Linux after running the first one):[code]sudo sensors-detect  
watch sensors
sudo pwmconfig
sudo service fancontrol start[/code]Then, go through the procedure below:
(1) Run the following command to create a script file.
[b][color=Red]sudo mkdir -p "/Fancontrol/" & sudo xed /Fancontrol/HDD_temp[/color][/b]
(2) Copy the following script into the file and save it.[code]#!/bin/bash
F=/Fancontrol/Hddtemp
while true
do
T=$(hddtemp -n /dev/sda)
echo "($T * 1000)/1" | bc > "$F".write
mv --force "$F".write "$F" && sleep 30
done[/code](3) Run the following command to make it executable.
[color=#ff0000][b]sudo chmod +x /Fancontrol/HDD_temp[/b][/color]
(4) Run the following command to create a service file.
[b][color=Red]sudo xed /lib/systemd/system/HDD_temp.service[/color][/b]
(5) Copy the following lines into the file and save it.[code][Service]
ExecStart=/Fancontrol/HDD_temp

[Install]
WantedBy=multi-user.target[/code](6) Run the following commands one by one:
[b][color=Red]sudo chmod 664 /lib/systemd/system/HDD_temp.service
sudo systemctl daemon-reload
sudo systemctl start HDD_temp.service
sudo systemctl enable HDD_temp.service [/color][/b]

Then, the script “[b][color=Red]HDD_temp[/color][/b]” will be run as a system service at Linux startup.
(7) Run the following command to edit “[b][color=Red]fancontrol[/color][/b]”, the configuration file.
[b][color=Red]sudo xed /etc/fancontrol[/color][/b]

Find the line that begins with “[b][color=Red]FCTEMPS[/color][/b]”. For example:
[b][color=Red]FCTEMPS=hwmon1/pwm1=hwmon1/temp1_input[/color][/b]

On that line, [b][color=Red]hwmon1/temp1_input[/color][/b] is the temperature (e.g. the chipset temperature) currently read by Fancontrol. Replace it with [b][color=Red]/Fancontrol/Hddtemp[/color][/b], and the line will become:
[b][color=Red]FCTEMPS=hwmon1/pwm1=/Fancontrol/Hddtemp[/color][/b]

Save the file and run the following command to restart Fancontrol.
[b][color=Red]sudo service fancontrol restart[/color][/b]

Then, the fan controlled by “[b][color=Red]hwmon1/pwm1[/color][/b]” will respond to “[b][color=Red]/Fancontrol/Hddtemp[/color][/b]”, the hard disk temperature. Note that "HDD_temp" and "Hddtemp" are the script file and output file respectively. Don't confuse them.

[[i] 本帖最後由 matthew_wai 於 2019-4-16 02:41 PM 編輯 [/i]]

路寅甲 2019-3-1 12:30 PM

UNIX script開頭要有#!, 以指定執行script的程式, 例如bash


script應放在PATH指定的Folder, /var/lib/不在PATH內, 且根據script內容睇, LOCATION=/var/lib/Fancontrol_Hddtemp是個directory, 並非script本身, 是用作記錄HDD溫度, 給fancontrol daemon參考


如要在啟動系統時自動執行該script, 另外要寫個systemd service file, 然後行systemctl enable <file>

matthew_wai 2019-3-1 02:13 PM

[b][color=Blue][url=https://computer.discuss.com.hk/viewthread.php?tid=28063756&page=1#pid495536111]See post #1.[/url][/color][/b]

[[i] 本帖最後由 matthew_wai 於 2019-3-6 03:57 PM 編輯 [/i]]

路寅甲 2019-3-1 03:06 PM

[quote]原帖由 [i]matthew_wai[/i] 於 2019-3-1 02:13 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=495569874&ptid=28063756][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
Thank you very much, 路寅甲.

I have just added “#!/bin/bash -e” atop the script, which works now. However, it reads “38000” when the actual temperature is 38°C. How should  ... [/quote]

可用

[b]echo $maximum | bc > "$FILE_MAX".write[/b]

但x1000可能是fancontrol所需的format, 可查一下temp1_input是否也x1000,




[root@host hwmon2]# cat /sys/class/hwmon/hwmon2/temp1_input

35000

matthew_wai 2019-3-1 03:31 PM

[quote]原帖由 [i]路寅甲[/i] 於 2019-3-1 03:06 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=495572160&ptid=28063756][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
[root@host hwmon2]# cat /sys/class/hwmon/hwmon2/temp1_input

35000  [/quote]
Sorry, I don't understand it. What should I do with it? Run it on Terminal or via a script?

路寅甲 2019-3-1 04:00 PM

command to check run time value of [quote]原帖由 [i]matthew_wai[/i] 於 2019-3-1 03:31 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=495573189&ptid=28063756][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]

Sorry, I don't understand it. What should I do with it? Run it on Terminal or via a script? [/quote]

Terminal,  command to check runtime value of temp1_input
        cat /sys/class/hwmon/hwmon2/temp1_input

[[i] 本帖最後由 路寅甲 於 2019-3-1 04:02 PM 編輯 [/i]]

matthew_wai 2019-3-1 04:04 PM

[color=#ff0000]matthew@matthew-pc:~$ cat /sys/class/hwmon/hwmon2/temp1_input[/color]
[color=#ff0000]35000[/color]

[[i] 本帖最後由 matthew_wai 於 2019-3-6 03:58 PM 編輯 [/i]]

路寅甲 2019-3-1 04:29 PM

commented out items relating to average calculation
also taken the maximum=0 out out while loop, so that $maximun is not cleared every loop

複製內容到剪貼板
代碼:

......
LOCATION=/var/lib/Fancontrol_Hddtemp
FILE_MAX="$LOCATION"/Fancontrol_Hddtemp-maximum
# FILE_AVG="$LOCATION"/Fancontrol_Hddtemp-average
DEGUG=0
[[ -e $LOCATION ]] || mkdir "$LOCATION"
maximum=0
while true
    do
    temperature=0
#    summation=0
#    average=0
#    count=0
    for value in $(hddtemp -n $DISKS 2>/dev/null)
    do
        [[ $value != [0-9]* ]] && continue
        temperature="${value//[!0-9]}"
#       ((summation+=$temperature))
#       ((count+=1))
        if [ $temperature -gt $maximum ]; then
            maximum=$temperature
        fi
        if [ $DEGUG = 1 ]; then
#           average=$(echo "scale=1; $summation / $count" | bc)
#            echo $summation
            echo $maximum
#            echo $average
#            echo $count
            echo "-----"
        fi
    done
#    average=$(echo "scale=1; $summation / $count" | bc)
    # fancontrol output format
    echo "($maximum * 1000)/1" | bc > "$FILE_MAX".write
#    echo "($average * 1000)/1" | bc > "$FILE_AVG".write
    mv --force "$FILE_MAX".write "$FILE_MAX"
#    mv --force "$FILE_AVG".write "$FILE_AVG"
    sleep 45
done
exit

路寅甲 2019-3-1 05:40 PM

[quote]原帖由 [i]路寅甲[/i] 於 2019-3-1 04:29 PM 發表 [url=https://www.discuss.com.hk/redirect.php?goto=findpost&pid=495575610&ptid=28063756][img]https://www.discuss.com.hk/images/common/back.gif[/img][/url]
commented out items relating to average calculation
also taken the maximum=0 out out while loop, so that $maximun is not cleared every loop

複製內容到剪貼板
代碼:

......
LOCATION=/var/lib/Fancontrol_Hd ... [/quote]

睇清maximum係指最高溫嗰隻disk, 所以拉出while loop係錯
要原來咁每個loop清零,留返係while里面

matthew_wai 2019-3-1 06:10 PM

[b][color=Blue][url=https://computer.discuss.com.hk/viewthread.php?tid=28063756&page=1#pid495536111]See post #1.[/url][/color][/b]

[[i] 本帖最後由 matthew_wai 於 2019-3-6 03:58 PM 編輯 [/i]]

matthew_wai 2019-3-1 09:08 PM

[b][color=Blue][url=https://computer.discuss.com.hk/viewthread.php?tid=28063756&page=1#pid495536111]See post #1.[/url][/color][/b]

[[i] 本帖最後由 matthew_wai 於 2019-3-6 03:59 PM 編輯 [/i]]

路寅甲 2019-3-2 09:15 AM

[quote]原帖由 [i]matthew_wai[/i] 於 2019-3-1 09:08 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=495587948&ptid=28063756][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]

Assuming "~/folder/HDD_temp" is the path to the script, how should I create such a file? The script has to be run before Fancontrol is started. Otherwise, Fancontrol has to be restarted. [/quote]

簡單起見, 我將個script同fancontrol package放埋一齊,  /usr/sbin/fancontrol-via-hddtemp

boot機時自動執行shell script嘅方法可參考
[url=https://linuxconfig.org/how-to-automatically-execute-shell-script-at-startup-boot-on-systemd-linux]https://linuxconfig.org/how-to-automatically-execute-shell-script-at-startup-boot-on-systemd-linux[/url]


係呢個case, 因為安裝fancontrol時已帶有systemd unit file, 可以試作為參考, (同上邊條link擺放unit file嘅direcoty不同, 無問題)

1. 睇 fancontrol 的 systemd unit file ( cat /lib/systemd/system/fancontrol.service )

[Unit]
Description=fan speed regulator
# Run pwmconfig to create this file.
ConditionPathExists=/etc/fancontrol
After=lm-sensors.service
Documentation=man:fancontrol(8) man:pwmconfig(8)

[Service]
ExecStartPre=/usr/sbin/fancontrol --check
ExecStart=/usr/sbin/fancontrol
PIDFile=/var/run/fancontrol.pid

[Install]
WantedBy=multi-user.target

2. 複製並修改做 /lib/systemd/system/fancontrol-via-hddtemp.service  

[Unit]
Description=fan speed regulator via hdd temp
After=lm-sensors.service
Before=fancontrol.service

[Service]
ExecStart=/usr/sbin/fancontrol-via-hddtemp
PIDFile=/var/run/fancontrol-via-hddtemp.pid

[Install]
WantedBy=multi-user.target

完成後好似上邊條link講咁reload systemd daemon ...
# systemctl daemon-reload
# systemctl enable fancontrol-via-hddtemp.service
# reboot

[[i] 本帖最後由 路寅甲 於 2019-3-2 02:33 PM 編輯 [/i]]

路寅甲 2019-3-2 09:17 AM

[quote]原帖由 [i]matthew_wai[/i] 於 2019-3-1 06:10 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=495580395&ptid=28063756][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
Assuming there are three disks, “maximum” refers to the hottest one, and “average” refers to their average. Am I right?
Please read the modified script below. Do you think the ... [/quote]
yes,
"average" is not used by fancontrol, you can delete all those items

matthew_wai 2019-3-2 03:58 PM

[b][color=Blue][url=https://computer.discuss.com.hk/viewthread.php?tid=28063756&page=1#pid495536111]See post #1.[/url][/color][/b]

[[i] 本帖最後由 matthew_wai 於 2019-3-6 04:01 PM 編輯 [/i]]

路寅甲 2019-3-2 05:11 PM

[quote]原帖由 [i]matthew_wai[/i] 於 2019-3-2 03:58 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=495621633&ptid=28063756][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]

I have created the following files:
/usr/sbin/fancontrol-via-hddtemp
/lib/systemd/system/fancontrol-via-hddtemp.service

Should I run the following commands on Terminal?
Why is there # before  ... [/quote]


#通常係bash prompt最後一個字母, 後面跟住嘅係正式command, 入command唔使#

所以第4個唔係command, 係第3個command執行後顯示嘅結果

第一句command如果個file係從其它service file複製出嚟再修改就唔使, 如果放係/lib/systemd/systm/內, 應跟返其它文件用644權限

第3個command你打漏咗enable :
   systemctl enable fancontrol-via-hddtemp.service

[[i] 本帖最後由 路寅甲 於 2019-3-2 05:18 PM 編輯 [/i]]

matthew_wai 2019-3-2 06:42 PM

[b][color=Blue][url=https://computer.discuss.com.hk/viewthread.php?tid=28063756&page=1#pid495536111]See post #1.[/url][/color][/b]

[[i] 本帖最後由 matthew_wai 於 2019-3-6 04:02 PM 編輯 [/i]]

matthew_wai 2019-3-2 08:47 PM

[b][color=Blue][url=https://computer.discuss.com.hk/viewthread.php?tid=28063756&page=1#pid495536111]See post #1.[/url][/color][/b]

[[i] 本帖最後由 matthew_wai 於 2019-3-6 04:03 PM 編輯 [/i]]

matthew_wai 2019-3-3 10:21 PM

[b][color=Blue][url=https://computer.discuss.com.hk/viewthread.php?tid=28063756&page=1#pid495536111]See post #1.[/url][/color][/b]

[[i] 本帖最後由 matthew_wai 於 2019-3-6 04:04 PM 編輯 [/i]]

路寅甲 2019-3-4 12:41 PM

[quote]原帖由 [i]matthew_wai[/i] 於 2019-3-3 10:21 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=495687633&ptid=28063756][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
The script will output "maximum".
When one HDD is connected, the output is 40000, which is normal.
When two HDDs are connected, the output is 1600000, which is impossible.
When I have removed the U ... [/quote]

我試咗插隻USB然後run個script係正常,
USB本身冇sensor, 但獲取溫度嘅指令會彈出一句
   [okla@host ~]$ hddtemp -n /dev/sdd 2>/dev/null
   /dev/sdd: Generic STORAGE DEVICE: S.M.A.R.T. not available
for loop會一個個詞咁check, 如果呢個output包含有數字, 就會被當作溫度處理, 出現錯誤結果

可試解決辦法是讓for loop將hddtemp輸出指令成個一齊check係唔係數字,
將separator定義為"new line", 而並非預定嘅"space"

#!/bin/bash -e
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin/
[color=red]IFS='
'[/color]
[color=#ff0000][color=black]......[/color][/color]

matthew_wai 2019-3-4 01:01 PM

[b][color=Blue][url=https://computer.discuss.com.hk/viewthread.php?tid=28063756&page=1#pid495536111]See post #1.[/url][/color][/b]

[[i] 本帖最後由 matthew_wai 於 2019-3-6 04:04 PM 編輯 [/i]]

路寅甲 2019-3-4 01:06 PM

[quote]原帖由 [i]matthew_wai[/i] 於 2019-3-4 01:01 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=495714027&ptid=28063756][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]

Do you mean the command is on two lines?

My USB HDD has a sensor.
My SATA HDD is "/dev/sda/". Is it possible to use only the "/dev/sda/" sensor to the exclusion of all others? If so, the script ... [/quote]

係2行, quote 個 new line,  留意兩個' '中間冇任何space, 只是個return分行

show嚟睇睇, 假設usb是/dev/sdb :
hddtemp -n /dev/sdb 2>/dev/null

[[i] 本帖最後由 路寅甲 於 2019-3-4 01:10 PM 編輯 [/i]]

matthew_wai 2019-3-4 01:14 PM

[b][color=Blue][url=https://computer.discuss.com.hk/viewthread.php?tid=28063756&page=1#pid495536111]See post #1.[/url][/color][/b]

[[i] 本帖最後由 matthew_wai 於 2019-3-6 04:05 PM 編輯 [/i]]

路寅甲 2019-3-4 01:24 PM

[quote]原帖由 [i]matthew_wai[/i] 於 2019-3-4 01:14 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=495714656&ptid=28063756][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
My SATA HDD is "/dev/sda".
My USB HDD is "/dev/sde".

matthew@matthew-pc:~$ hddtemp -n /dev/sda 2>/dev/null
matthew@matthew-pc:~$ hddtemp -n /dev/sde 2>/dev/null
matthew@matthew-pc:~$  ... [/quote]


[okla@host ~]$ hddtemp -n /dev/sda 2>/dev/null
33
[okla@host ~]$ hddtemp -n /dev/sdb 2>/dev/null
33
[okla@host ~]$ hddtemp -n /dev/sdc 2>/dev/null
32
[okla@host ~]$ hddtemp -n /dev/sdd 2>/dev/null
/dev/sdd: Generic STORAGE DEVICE: S.M.A.R.T. not available

個command係script內用作獲取HDD溫度, 如果你部機出唔到, 硬件有問題?

路寅甲 2019-3-4 01:30 PM

若只想用sda溫度, 將DISKS改下, 其餘部份雖有多餘coding, 可留以後有慢慢研究

DISKS="/dev/sda"

或將while loop簡化下,


while true
    do
    temperature=$(hddtemp -n /dev/sda 2>/dev/null)
    [[ $temperature != [0-9]* ]] && continue
    # fancontrol output format
    echo "($temperature * 1000)/1" | bc > "$FILE_MAX".write
    mv --force "$FILE_MAX".write "$FILE_MAX"
    sleep 45
done

[[i] 本帖最後由 路寅甲 於 2019-3-4 01:45 PM 編輯 [/i]]

matthew_wai 2019-3-4 02:08 PM

[quote]原帖由 [i]路寅甲[/i] 於 2019-3-4 01:24 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=495715112&ptid=28063756][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
個command係script內用作獲取HDD溫度, 如果你部機出唔到, 硬件有問題?[/quote]
See the screenshot below.
[b][color=#ff0000]WDC WD1600BEVS-22UST0[/color][/b] is my USB HDD.
[b][color=#ff0000]ST3500418AS[/color][/b] is my SATA HDD.
[attach]9395254[/attach]

[[i] 本帖最後由 matthew_wai 於 2019-3-4 02:09 PM 編輯 [/i]]

路寅甲 2019-3-4 02:24 PM

[quote]原帖由 [i]matthew_wai[/i] 於 2019-3-4 02:08 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=495717116&ptid=28063756][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]

See the screenshot below.
WDC WD1600BEVS-22UST0 is my USB HDD.
ST3500418AS is my SATA HDD.
9395254 [/quote]


How about :



[okla@host ~]$ hddtemp /dev/sda
/dev/sda: KINGSTON SUV400S37240G: 33°C
[okla@host ~]$ hddtemp /dev/sdd
/dev/sdd: Generic STORAGE DEVICE: S.M.A.R.T. not available

matthew_wai 2019-3-4 02:24 PM

My SATA HDD is "[b][color=#ff0000]/dev/sda[/color][/b]".
My USB HDD is "[b][color=#ff0000]/dev/sde[/color][/b]".
[b][color=#ff0000][b][color=#ff0000][/color][/b]
matthew@matthew-pc:~$ sudo hddtemp -n /dev/sda
38
matthew@matthew-pc:~$ sudo hddtemp -n /dev/sde
/dev/sde: WD 1600BEV External: S.M.A.R.T. not available[/color][/b]

matthew_wai 2019-3-4 02:27 PM

[b][color=#ff0000]matthew@matthew-pc:~$ sudo hddtemp /dev/sda
/dev/sda: ST3500418AS: 38°C
matthew@matthew-pc:~$ sudo hddtemp /dev/sde
/dev/sde: WD 1600BEV External: S.M.A.R.T. not available[/color][/b]

路寅甲 2019-3-4 02:32 PM

[quote]原帖由 [i]matthew_wai[/i] 於 2019-3-4 02:27 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=495718019&ptid=28063756][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
matthew@matthew-pc:~$ sudo hddtemp /dev/sda
/dev/sda: ST3500418AS: 38°C
matthew@matthew-pc:~$ sudo hddtemp /dev/sde
/dev/sde: WD 1600BEV External: S.M.A.R.T. not available [/quote]

It is normal now, U can see, there is a number 1600 in the /dev/sde output
So you can try the above mentioned solution

matthew_wai 2019-3-4 05:08 PM

[b][color=Blue][url=https://computer.discuss.com.hk/viewthread.php?tid=28063756&page=1#pid495536111]See post #1.[/url][/color][/b]

[[i] 本帖最後由 matthew_wai 於 2019-3-6 04:06 PM 編輯 [/i]]
頁: [1] 2
查看完整版本: Fancontrol according to hard disk temperature 使 Linux 根據硬碟溫度控制風扇轉速