SantaRosaFanControl

Differences between revisions 3 and 4
Revision 3 as of 2007-10-08 08:21:38
Size: 6048
Editor: mx
Comment: make the script a daemon
Revision 4 as of 2007-10-08 08:25:16
Size: 5640
Editor: mx
Comment:
Deletions are marked like this. Additions are marked like this.
Line 108: Line 108:
# modified by nick barcet nicolasNOSPAM AT barcet DOT com # modified by nick barcet nickNOSPAM AT barcet DOT com
Line 177: Line 177:
  nope)
 #
 # If the "reload" option is implemented then remove the
 # 'force-reload' alias
 #
 log_daemon_msg "Restarting $DESC" "$NAME"
 do_stop
 case "$?" in
   0|1)
  do_start
  case "$?" in
   0) log_end_msg 0 ;;
   1) log_end_msg 1 ;; # Old process is still running
   *) log_end_msg 1 ;; # Failed to start
  esac
  ;;
   *)
    # Failed to stop
  log_end_msg 1
  ;;
 esac
 ;;

Finding that my MacBookPro 3.1 (MacBookPro/SantaRosa) was getting incredibly hot, even with applesmc installed, I have written a stupid little script that controls the minimum fan speed so that it stays at a reasonable temperature.

I am looking at how to make it so that this is taken care of by ACPI. If you can help me, contact Nbarcet.

The following scripts create a daemon that will regulate temperature for us. Create it as /usr/local/sbin/tempmod.

# A temperature daemon to help for a cooler MacBookPro 3.1
# Author nick barcet nickNOSPAM AT barcet DOT com

do_init()
{
        # I'm pretty happy with the following  values but feel free to
        # play with them.
        # We set different value for on and off line, as we don't care
        # about saving power when on-line.
        if (cat /proc/acpi/ac_adapter/ADP1/state | grep on-line > /dev/null); then
                # We are online, be pretty cool
                MIN=465 # temp in degree celsius x 10
                MAX=520
                let "RATIO = 6000 / ($MAX - $MIN)"
        else
                [ $debug -eq 1 ] && echo "state:                   off-line"
                MIN=510 # temp in degree celsius x 10
                MAX=590
                let "RATIO = 4500 / ($MAX - $MIN)"
        fi
        let "TARGET = ( ($MAX - $MIN)/3 ) + $MIN"
        let "HIGH = ( ($MAX - $MIN)/4 ) + $TARGET"
}

debug=0
do_init
count=1


while [ true ]; do 
        TEMP1=`cat /sys/devices/platform/applesmc.768/temp5_input`
        TEMP2=`cat /sys/devices/platform/applesmc.768/temp8_input`
        TEMP3=`cat /sys/devices/platform/applesmc.768/temp3_input`
        TEMP4=`cat /sys/devices/platform/applesmc.768/temp3_input`
        let "TEMP = ($TEMP1 + $TEMP2 + $TEMP3 + $TEMP4)/ 400"
        let "TEMP = ($TEMP1 + $TEMP2)/200"

        [ $debug -eq 1 ] && echo "average temp: $TEMP (target: $TARGET, high: $HIGH)" 
        
        let "SPEED = ($TEMP - $MIN) * $RATIO"    

        # Just to make sur we do not set fan >6000 or <0
        if [ $SPEED -gt 6000 ]; then
                SPEED=6000
        elif [ $SPEED -lt 1 ]; then
                SPEED=1
        elif [ $TEMP -lt $TARGET ]; then
                #let autoregulation work (if it does)
                SPEED=1  
        fi

        [ $debug -eq 1 ] && echo "set fan speed to: $SPEED (ratio: $RATIO)"
        echo $SPEED > /sys/devices/platform/applesmc.768/fan2_min
        echo $SPEED > /sys/devices/platform/applesmc.768/fan1_min
        
        if [ $SPEED -lt 2 ]; then
                # Sleep longer when autoregulatings
                sleep 20
                let "count = $count+4"
        else
                sleep 5
                let "count = $count+1"
        fi

        if [ $count -gt 10 ]; then
                count=0
                do_init
                # if above HIGH temp level, force the fan speed a bit
                if [ $TEMP -gt $HIGH ]; then
                        [ $debug -eq 1 ] && echo "temp: $TEMP > target: $TARGET"
                        [ $debug -eq 1 ] && echo "Force fan speed to: 6000 for 20 sec"
                        echo 6000 > /sys/devices/platform/applesmc.768/fan2_min
                        echo 6000 > /sys/devices/platform/applesmc.768/fan1_min
                        sleep 20
                fi
        fi
done

allow access rights to it by doing a :

sudo chmod +x /usr/local/sbin

then add the following script as /etc/init.d/tempmon.sh :

### BEGIN INIT INFO
# Provides:          tempmon.sh
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      S 0 1 6
# Short-Description: Sets the MacBook minimum fan speed
# Description:       Uses the applesmc kernel module to set the minimum
#                    fan speed that the Mac will leave the fan at.
### END INIT INFO

# Author: Jason Parekh <jasonparekh@gmail.com> 
# modified by nick barcet nickNOSPAM AT barcet DOT com

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/usr/sbin:/usr/bin:/sbin:/bin
DESC="MacBook minimum fan speed setter"
NAME=tempmon
SCRIPTNAME=/etc/init.d/$NAME
MIN_FAN_SPEED=3000
DEFAULT_MIN_FAN_SPEED=2000

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
[ -f /etc/default/rcS ] && . /etc/default/rcS

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
        killall tempmon &> /dev/null
        /usr/local/sbin/tempmon > /dev/null &
}

#
# Function that stops the daemon/service
#
do_stop()
{
        killall tempmon&> /dev/null
        sleep 1
        # Put the fan back to auto regulation
        echo 1 > /sys/devices/platform/applesmc.768/fan2_min
        echo 1 > /sys/devices/platform/applesmc.768/fan1_min
}

case "$1" in
  start)
        [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
        do_start
        case "$?" in
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
  stop)
        [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
        do_stop
        case "$?" in
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
  reload|force-reload|restart)
        #
        # If do_reload() is not implemented then leave this commented out
        # and leave 'force-reload' as an alias for 'restart'.
        #
        log_daemon_msg "Reloading $DESC" "$NAME"
        do_start
        log_end_msg 0
        ;;
  *)
        echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
        echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
        exit 3
        ;;
esac

:

allow access rights to it by doing a :

sudo chmod +x /etc/init.d/tempmon.sh

make sure that it starts and stops automatically :

sudo update-rc.d tempmon.sh defaults 90

you can then start and stop the daemon by using the following call

sudo /etc/init.d/tempmon.sh {start|stop|reload} 

MacBookPro/SantaRosaFanControl (last edited 2011-04-21 11:08:42 by mx)