因為自己在使用的ThinkPad T440p的芯片散熱實在是SB至極,為了守衛本本的安全,為了世界的和平……不說那么多了,總之為了方便,自己寫了一個溫控腳本,根據不同的溫度來控制風扇轉速。別和我說有thinkfan什么的,那玩意兒在這里不管用——它拒絕執行……不管你用什么辦法,它就是拒絕的!
反正本人也懶,懶得找什么論壇解決那些奇葩的bug與設置問題,所以直接自己動手,豐衣足食,寫了一個腳本。雖然不高明,但總比每次開機自己手動更改要好一些,不然一不小心電腦就煎蛋了。
所需內容
也很簡單,需要:
- 獲取對風扇速度調整的權限。ThinkPad用戶參考這里或者thinkwiki,稍后會介紹如何操作。
- lm_sensors,用來讀取溫度
- linux基本命令的head、sed以及awk,用于獲取設備溫度。
- crontab服務以便于自動執行
思路
通過sensors檢測cpu溫度,根據溫度的不同來向/proc/acpi/ibm/fan寫入不同的級別來控制風扇轉速。
echo level [1-5,7,full-speed] > /proc/acpi/ibm/fan
具體命令方式參考fan文件內容——前提是你獲得了對風扇控制的權限。
然后將腳本加入到crontab任務——必須是root用戶權限,每個幾分鐘執行一次腳本檢測并調整風扇。
方法
獲取對風扇速度調整的權限(以ThinkPad為例)
首先使用root用戶,在 /etc/modprobe.d/ 目錄下創建一個文件:
root@localhost:~ # vi /etc/modprobe.d/thinkpad_acpi.conf
內容如下:
options thinkpad_acpi experimental=1 fan_control=1
加載對風扇控制模塊:
root@localhost:~ # modprobe thinkpad_acpi fan_control=1 experimental=1
然后進入到 /proc/acpi/ibm/fan中看看是否可以更改,以本人為例,如果可以更改的話內容如下:
status: enabled
speed: 3426
level: auto
commands: level <level> (<level> is 0-7, auto, disengaged, full-speed)
commands: enable, disable
commands: watchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))
當然了,不同的發行辦可能有些許不同,但使一般都會提供給commands以供選擇,否則您沒有修改權限。
手動更改級別的話執行如下命令:
root@localhost:~ # echo level 5 > /proc/acpi/ibm/fan
以上命令使風扇以級別5的轉速運轉。一般數字越大風扇速度越快。有的發行版沒有級別6,而且命令模式也可能不一樣。本人使用的是opensuse,命令中可以執行級別6的控制,在腳本中設置成注釋,想使用的話設置好數值取消注釋即可。
創建腳本,放到一個自己喜歡的地方
可能會放在你的家目錄下的某一個地方,或者/etc/的某一個地方,隨你喜好——只要你自己覺得合理即可。
腳本如下:
#!/bin/bash
cpu_temp=$(sensors -u|sed -n '/Core 0/ {n;p}'|awk {'print $2'}|sed 's/.000//')
#-------------configure file------------
#You can modify these arguments in this area by yourself.
#Example:
# lv5=45 #It means that the fan will be running at level 5 speed when CPU temperature exceeds 45 digrees Celsius.
#If the command 'sensors' returns the value in Fahrenheit,you have to modify these arguments as Fahenheit,too.
lv1=30
lv2=35
lv3=40
lv4=42
lv5=45
#lv6= #Setting a vaild value and deleting '#' in the relevent code snippet if you want to use level 6.
lv7=50
lv_max=70 #full speed
#--------------end of configure file--------------
change_fan_level() {
echo level $1 > /proc/acpi/ibm/fan
}
if [ $cpu_temp -gt $lv_max ]
then
change_fan_level full-speed
elif [ $cpu_temp -gt $lv7 ]
then
change_fan_level 7
#Deleting '#' in the following code snippet and set a vaild value for argument lv6 if you want to use level 6.
#elif [ $cpu_temp -gt $lv6 ]
#then
#change_fan_level 6
elif [ $cpu_temp -gt $lv5 ]
then
change_fan_level 5
elif [ $cpu_temp -gt $lv4 ]
then
change_fan_level 4
elif [ $cpu_temp -gt $lv3 ]
then
change_fan_level 3
elif [ $cpu_temp -gt $lv2 ]
then
change_fan_level 2
elif [ $cpu_temp -gt $lv1 ]
then
change_fan_level 1
else
change_fan_level 1
fi
求別吐槽我的蹩腳英文和這糟糕的代碼縮進風格了……誰讓人家是小白~的呢。
然后賦予其可執行權限。
root@localhost:~ # chmod a+x file.sh
添加進任務列表
編輯root的crontab列表。
root@localhosts:~ # crontab -e
打開后是一個vi編輯器,添加新的一行,內容如下:
*/2 * * * * /bin/bash /shell_script_path
注意,這里腳本的路徑是絕對路徑。
這樣便每隔2分鐘便執行腳本,來修改風扇策略。
后記
還不是因為thinkfan搞不定所以才出此下策,盡量不推薦如此調教。畢竟腳本的執行能力你懂的,而且是靠crontab來實現動態檢測調整的。除非實在是沒有辦法,否則不推薦用這個方法。
另外啊,我的sensors有些坑爹的每次重啟對于溫度的標識都有所改變,所以不得不直接認定Core 0核心的溫度為準。通配符什么的復雜好用,一時間想不起來了,所以根據這位大觸的方法來定位cpu核心0的溫度。