macfan - fancontrol for Mac

Submitted scripts and programs
Forum rules
Your own work only.
machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

macfan - fancontrol for Mac

Unread post by machinebacon » Thu Nov 10, 2016 3:27 pm

I just wanted a quick and dirty way of setting the Macbook fan to the lowest or highest setting according to the allowed values, pretty much like cpufreq-utils would do with the CPU. Put the following into /usr/local/bin or so. Chmod a+x it. Then add it to the sudoers file if you want to run it without password.

Code: Select all

#!/bin/bash
# macfan - fancontrol for Mac
# usage: 
#   macfan l - set fans to lowest possible speed
#   macfan h - set fans to highest possible speed

# set the path to the fans
SMC="/sys/devices/platform/applesmc.768"
# read lowest and highest possible values
LO1=`cat $SMC/fan1_min`
LO2=`cat $SMC/fan2_min`
HI1=`cat $SMC/fan1_max`
HI2=`cat $SMC/fan2_max`
# set fans to manual control
echo 1 > $SMC/fan1_manual
echo 1 > $SMC/fan2_manual
# read from the input
case $1 in
l)
	echo $LO1 > $SMC/fan1_output
	echo $LO2 > $SMC/fan2_output
	echo "Setting fan to $LO1/$LO2 rpm" ;;
h)
	echo $HI1 > $SMC/fan1_output
	echo $HI2 > $SMC/fan2_output
	echo "Setting fan to $HI1/$HI2 rpm" ;;
*)
	echo "$0 l - set the fan speed to $LO1/$LO2 rpm"
        echo "$0 h - set the fan speed to $HI1/$HI2 rpm" && exit ;;
esac
..gnutella..

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: macfan - fancontrol for Mac

Unread post by wuxmedia » Thu Nov 10, 2016 4:48 pm

hot - or cold. take it or leave it :)
I like it :)
They basically are on full blast - when it's working hard *cof*xvidz*cof* or silent while some gentle emacsing
"Seek, and Ye shall find"
"Github | Chooons | Site"

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: macfan - fancontrol for Mac

Unread post by machinebacon » Thu Nov 10, 2016 4:58 pm

^ yeah actually you can simply replace the last option like

Code: Select all

*)
  echo $1 > $SMC/fan1_output
  echo $1 > $SMC/fan2_output
  echo "Settings fan to $1 rpm at your own risk!" ;;
To be honest, I have the setting mostly on 2000 rpm, and only when I feel the keyboard getting hot I blow it on 6000 for a minute or two.
..gnutella..

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: macfan - fancontrol for Mac

Unread post by machinebacon » Thu Nov 10, 2016 5:09 pm

Yeah okay, here's the more verbose version:

Code: Select all

#!/bin/bash

SMC="/sys/devices/platform/applesmc.768"

LO1=`cat $SMC/fan1_min`
LO2=`cat $SMC/fan2_min`
HI1=`cat $SMC/fan1_max`
HI2=`cat $SMC/fan2_max`

echo 1 > $SMC/fan1_manual
echo 1 > $SMC/fan2_manual


case $1 in

l)
	echo $LO1 > $SMC/fan1_output
	echo $LO2 > $SMC/fan2_output
	echo "Setting fan to $LO1 and $LO2" ;;
h)
	echo $HI1 > $SMC/fan1_output
	echo $HI2 > $SMC/fan2_output
	echo "Setting fan to $HI1 and $HI2" ;;
v)
	echo "$0 l - set the fan speed to $LO1/$LO2 rpm"
	echo "$0 h - set the fan speed to $HI1/$HI2 rpm" && exit ;;
*)
	if [ -z "$1" ]; then
		echo "Value missing."
		echo "Ranges: $LO1 - $HI1 (fan 1), $LO2 - $HI2 (fan 2)" && exit
	fi
	if [ "$1" -gt "$HI1" ]; then
		echo "Value out of range for fan 1 (max: $HI1)" && exit
	fi
	if [ "$1" -gt "$HI2" ]; then
		echo "Value out of range for fan 2 (max: $HI2)" && exit
	fi
   	if [ "$1" -lt "$LO1" ]; then
		echo "Value out of range for fan 1 (min: $LO1)" && exit
	fi
	if [ "$1" -lt "$LO2" ]; then
		echo "Value out of range for fan 2 (min: $LO2)" && exit
	fi


	echo $1 > $SMC/fan1_output
	echo $1 > $SMC/fan2_output
	echo "Settings fan to $1 rpm at your own risk!" ;;

esac
..gnutella..

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: macfan - fancontrol for Mac

Unread post by machinebacon » Thu Nov 10, 2016 5:58 pm

and a version that can be bound to two keys via xbindkeys or sxhkd. Edit the steps if you want.

Code: Select all

#!/bin/bash
SMC="/sys/devices/platform/applesmc.768"

step=250

min1=`cat $SMC/fan1_min`
min2=`cat $SMC/fan2_min`
max1=`cat $SMC/fan1_max`
max2=`cat $SMC/fan2_max`

echo 1 > $SMC/fan1_manual
echo 1 > $SMC/fan2_manual

actual1=`cat $SMC/fan1_output`
actual2=`cat $SMC/fan2_output`

# for dunst etc.
notify-send "Fan speed set to $actual1/$actual2"

case $1 in

i)
	newvalue_1=$(( $step + $actual1 ))
	newvalue1=${newvalue_1}
	newvalue_2=$(( $step + $actual2 ))
	newvalue2=${newvalue_2}

	if (( newvalue1 > $max1 )); then
		echo "New value $newvalue1 exceeds maximum value $max1" && exit
	fi
	if (( newvalue2 > $max2 )); then
		echo "New value $newvalue1 exceeds maximum value $max1" && exit
	fi

	echo "Fan 1: $newvalue1 OK."
	echo "Fan 2: $newvalue2 OK." ;;
d)
	newvalue_1=$(( $actual1 - $step ))
	newvalue1=${newvalue_1}
	newvalue_2=$(( $actual2 - $step))
	newvalue2=${newvalue_2}

	if (( newvalue1 < $min1 )); then
		echo "New value $newvalue1 exceeds minimum value $min1" && exit
	fi
	if (( newvalue2 < $min2 )); then
		echo "New value $newvalue1 exceeds minimum value $min2" && exit
	fi

	echo "Fan 1: $newvalue1 OK."
	echo "Fan 2: $newvalue2 OK." ;;
*)
	echo "Fan 1: $actual1"
	echo "Fan 2: $actual2"
	exit ;;

esac

echo $newvalue1 > $SMC/fan1_output
echo $newvalue2 > $SMC/fan2_output
..gnutella..

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: macfan - fancontrol for Mac

Unread post by wuxmedia » Fri Nov 11, 2016 7:27 am

wait, so obviously - it doesn't control fans ootb?
weird
"Seek, and Ye shall find"
"Github | Chooons | Site"

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: macfan - fancontrol for Mac

Unread post by machinebacon » Fri Nov 11, 2016 8:19 am

it only knows full speed, so no control ootb (Macbook 4,1 on sid)
..gnutella..

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: macfan - fancontrol for Mac

Unread post by wuxmedia » Sun Nov 13, 2016 7:26 pm

just a thought - you could rename it 'mac fanboi'...
"Seek, and Ye shall find"
"Github | Chooons | Site"

Post Reply