How can I adjust fan speed according to hard drive temperature via Fancontrol?
2 Answers
I finally found a simple script to control fan speed according to hard drive temperature via Fancontrol, Hddtemp, and Lm-sensors. In the following script, “/dev/sda” is the hard disk to be monitored, and “/Fancontrol/Hddtemp” is the output file to be read by Fancontrol. Press Ctrl + Alt + T to open Terminal and run the following command to check whether “/dev/sda” is the correct one:
sudo hddtemp /dev/sd[a-z]
Use only the one supported by Hddtemp, which will display the temperature rather than “S.M.A.R.T. not available”.
Replace “/dev/sda” with the correct one in the script if necessary.
If you have not yet configured Fancontrol, see this page, this page, and this page and run the following commands one by one (restart Linux after running the first one):
sudo sensors-detect
watch sensors
sudo pwmconfig
sudo service fancontrol start
Then, go through the procedure below:
(1) Run the following command to create a script file.
sudo mkdir -p "/Fancontrol/" & sudo xed /Fancontrol/HDD_temp
(2) Copy the following script into the file and save it.
#!/bin/bash
File=/Fancontrol/Hddtemp
while true
do
temperature=$(sudo hddtemp -n /dev/sda)
echo $(($temperature * 1000)) > "$File"
sleep 30
done
(3) Run the following command to make it executable.
sudo chmod +x /Fancontrol/HDD_temp
(4) Run the following command to create a service file.
sudo xed /lib/systemd/system/HDD_temp.service
(5) Copy the following lines into the file and save it.
[Service]
ExecStart=/Fancontrol/HDD_temp
[Install]
WantedBy=multi-user.target
(6) Run the following commands one by one:
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
Then, the script “HDD_temp” will be run as a system service at Linux startup.
(7) Run the following command to edit “fancontrol”, the configuration file.
sudo xed /etc/fancontrol
Find the line that begins with “FCTEMPS”. For example:
FCTEMPS=hwmon1/pwm1=hwmon1/temp1_input
On that line, “hwmon1/temp1_input” is the temperature (e.g. the chipset temperature) currently read by Fancontrol. Replace it with “/Fancontrol/Hddtemp”, and the line will become:
FCTEMPS=hwmon1/pwm1=/Fancontrol/Hddtemp
Save the file and run the following command to restart Fancontrol.
sudo service fancontrol restart
Then, the fan controlled by “hwmon1/pwm1” will respond to “/Fancontrol/Hddtemp”, the hard disk temperature. Note that "HDD_temp" and "Hddtemp" are the script file and output file respectively. Don't confuse them.
-
After Ctrl+Alt+T I bet you need to install fancontrol and lm-sensors :-p– beginerApr 16, 2019 at 7:34
-
In my answer, "Fancontrol", "Hddtemp", and "Lm-sensors" are hypertext links to their download pages. I assume readers know how to install them. Apr 16, 2019 at 11:35
I found Mathew Wai's answer really good, but I ran into some issues with drives sleeping and temperature data corruption. This has been stable for me:
FILE=/tmp/Hddtemp
while true
do
temperature=$(hddtemp -n /dev/sdc 2>/dev/null)
# Set base temp if we got nothing in stdout, this mean we are asleep
if [ "$temperature" == "" ]; then temperature=30; fi
# Get a tempfile to write out temp
NEW=$(mktemp /tmp/Hddtemp.XXXX)
echo $(($temperature * 1000)) > $NEW
# Handle case of initial/clean run
if [ -e $FILE ]; then
CLEAN="rm -f $(readlink -f $FILE)"
fi
# Atomic symlink replacement
ln -s $NEW $FILE.lnk
mv -T $FILE.lnk $FILE
# Clean old link, if it existed
$CLEAN
sleep 30
done
Given that hddtemp is going away in the next Debian release, I've changed mine to use "drivetemp", which can be read from sensors so I'll replace:
temperature=$(hddtemp -n /dev/sdc 2>/dev/null)
with
temperature=$(sensors -A drivetemp-scsi-5-0|grep temp1|tr -s ' '|cut -d '+' -f 2|cut -d '.' -f1)
I am just starting to use this sensors method, so I can't say for sure that it is stable.
which hddfancontrol
as your normal user might yield the information you're looking for; it needs to be somewhere like /usr/local/bin. If you cannot find it, try runninglocate hddfancontrol
orfind / -iname *hddfancontrol*