root/lm-sensors/trunk/prog/pwm/fancontrol @ 1798

Revision 1798, 5.2 KB (checked in by peglags, 12 years ago)

Added PID file creation.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1#!/bin/bash
2#
3# Simple script implementing a temperature dependent fan speed control
4#
5# Version 0.5
6#
7# Usage: fancontrol [CONFIGFILE]
8#
9# Dependencies:
10#   bash, awk, egrep, sed, lm-sensors :)
11#
12# Please send me any comments/suggestions to: marius.reiner@hdev.de Thanks!
13#
14#
15# Please be careful when using the fan control features of your mainboard, in
16# addition to the risk of burning your CPU, at higher temperatures there will
17# be a higher wearout of your other hardware components, too. So if you plan to
18# use these components in 20 years, maybe you shouldn't use fancontrol/your
19# hardware at all.
20#
21#
22#    Copyright 2003 Marius Reiner <marius.reiner@hdev.de>
23#
24#    This program is free software; you can redistribute it and/or modify
25#    it under the terms of the GNU General Public License as published by
26#    the Free Software Foundation; either version 2 of the License, or
27#    (at your option) any later version.
28#
29#    This program is distributed in the hope that it will be useful,
30#    but WITHOUT ANY WARRANTY; without even the implied warranty of
31#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32#    GNU General Public License for more details.
33#
34#    You should have received a copy of the GNU General Public License
35#    along with this program; if not, write to the Free Software
36#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
37#
38#
39
40echo $$ > /var/run/fancontrol.pid
41
42function LoadConfig {
43        echo "Loading configuration from $1 ..."
44        #grep configuration from file
45        INTERVAL=`egrep '^INTERVAL=.*$' $1 | sed -e 's/INTERVAL=//g'`
46        FCTEMPS=`egrep '^FCTEMPS=.*$' $1 | sed -e 's/FCTEMPS=//g'`
47        MINTEMP=`egrep '^MINTEMP=.*$' $1 | sed -e 's/MINTEMP=//g'`
48        MAXTEMP=`egrep '^MAXTEMP=.*$' $1 | sed -e 's/MAXTEMP=//g'`
49        MINSTART=`egrep '^MINSTART=.*$' $1 | sed -e 's/MINSTART=//g'`
50        MINSTOP=`egrep '^MINSTOP=.*$' $1 | sed -e 's/MINSTOP=//g'`
51
52        if [ "$INTERVAL" = "" ]
53        then
54                echo "Some settings missing..."
55                exit 1
56        fi
57
58
59        echo
60        echo "Common settings:"
61        echo "  INTERVAL=$INTERVAL"
62                                               
63        #write values to arrays
64        let fcvcount=0
65        for fcv in $FCTEMPS
66        do
67                AFCPWM[$fcvcount]=`echo $fcv |cut -d'=' -f1`
68                AFCTEMP[$fcvcount]=`echo $fcv |cut -d'=' -f2`
69                AFCMINTEMP[$fcvcount]=`echo $MINTEMP |sed -s 's/ /\n/g' |egrep "${AFCPWM[$fcvcount]}" |cut -d'=' -f2`
70                AFCMAXTEMP[$fcvcount]=`echo $MAXTEMP |sed -s 's/ /\n/g' |egrep "${AFCPWM[$fcvcount]}" |cut -d'=' -f2`
71                AFCMINSTART[$fcvcount]=`echo $MINSTART |sed -s 's/ /\n/g' |egrep "${AFCPWM[$fcvcount]}" |cut -d'=' -f2`
72                AFCMINSTOP[$fcvcount]=`echo $MINSTOP |sed -s 's/ /\n/g' |egrep "${AFCPWM[$fcvcount]}" |cut -d'=' -f2`
73                echo
74                echo "Settings for ${AFCPWM[$fcvcount]}:"
75                echo "  Depends on ${AFCTEMP[$fcvcount]}"
76                echo "  MINTEMP=${AFCMINTEMP[$fcvcount]}"
77                echo "  MAXTEMP=${AFCMAXTEMP[$fcvcount]}"
78                echo "  MINSTART=${AFCMINSTART[$fcvcount]}"
79                echo "  MINSTOP=${AFCMINSTOP[$fcvcount]}"
80                let fcvcount=fcvcount+1
81        done
82        echo
83}
84
85if [ -f "$1" ]
86then
87        LoadConfig $1
88else
89        LoadConfig /etc/fancontrol
90fi
91
92cd /proc/sys/dev/sensors/
93
94if (( $? != 0 )) ; then
95  echo "Directory /proc/sys/dev/sensors/ does not exist, did you load the necessary kernel modules?"
96  exit 1
97fi
98
99function calc () { awk "BEGIN { print $@ }"; }
100
101function UpdateFanSpeeds {
102        let fcvcount=0
103        while (( $fcvcount < ${#AFCPWM[@]} ))
104        do
105                #hopefully shorter vars will improve readability:
106                pwmo=${AFCPWM[$fcvcount]}
107                tsens=${AFCTEMP[$fcvcount]}
108                mint=${AFCMINTEMP[$fcvcount]}
109                maxt=${AFCMAXTEMP[$fcvcount]}
110                minsa=${AFCMINSTART[$fcvcount]}
111                minso=${AFCMINSTOP[$fcvcount]}
112                tval=`cat ${tsens} |cut -d' ' -f3 |cut -d'.' -f1`
113                pwmpval=`cat ${pwmo}`
114               
115                # debug info
116                #echo "pwmo=$pwmo"
117                #echo "tsens=$tsens"
118                #echo "mint=$mint"
119                #echo "maxt=$maxt"
120                #echo "minsa=$minsa"
121                #echo "minso=$minso"
122                #echo "tval=$tval"
123                #echo "pwmpval=$pwmpval"
124               
125                if (( $tval <= $mint ))
126                  then pwmval=0
127                elif (( $tval >= $maxt ))
128                  then pwmval=255
129                else
130                  pwmval=`calc "((10/(${maxt}-${mint})*(${tval}-${mint}))^2/1000*(${maxt}-${mint})*(255-${minso})+${minso})" |cut -d'.' -f1`
131                  if (( $pwmpval == 0 )) # if fan was stopped, start it
132                  then
133                        echo $minsa > $pwmo
134                        sleep 1
135                  fi
136                fi
137                echo $pwmval > $pwmo
138                fcvcount=$fcvcount+1
139        done
140}
141
142while true
143do
144        UpdateFanSpeeds
145        sleep $INTERVAL
146done
147
148# some old stuff/missing features, will clean this up soon
149#if ( test "$regulationtype" = "quad" ) ; then
150#  while true ; do
151#    temp=`cat ${temp1} | cut -b 12-13`
152#    if (( ${temp} < ${mintemp} )) ;
153#       then pwm=0
154#    elif (( ${temp} > ${maxtemp} )) ;
155#       then pwm=255
156#    else
157#       pwm=`calc "((10/(${maxtemp}-${mintemp})*(${temp}-${mintemp}))^2/1000*(${maxtemp}-${mintemp})*(255-${minspeed})+${minspeed})"`
158#       #no optimization here for readability (or sloth :))
159#    fi ;
160#    echo $pwm > ${pwm1} ;
161#
162#    sleep 10 ;
163#  done ;
164
165#elif ( test "$regulationtype" = "exp" ) ; then
166  #add support for exponential calculation here
167
168#else
169#  pwmconst=$[(255-${minspeed})/(${maxtemp}-${mintemp})]
170#  while true ; do
171#    let temp=`cat temp1 | cut -b 12-13` ;
172#    if (( ${temp} < ${mintemp} )) ;
173#        then pwm=0
174#    elif (( ${temp} > ${maxtemp} )) ;
175#        then pwm=255
176#    else
177#       pwm=$[(${temp}-${mintemp})*${pwmconst}+${minspeed}]
178#    fi ;
179#    echo $pwm > pwm2 ;
180#
181#    sleep 10 ;
182#  done ;
183#fi
Note: See TracBrowser for help on using the browser.