-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfanboost.sh
executable file
·248 lines (188 loc) · 6.57 KB
/
fanboost.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/bin/bash
# Script name
SCRIPT_NAME="mac_fan_booster"
# Path to the core temperature system sensor
CORE_SENSOR="/sys/devices/platform/coretemp.0/hwmon/hwmon1/temp1_input"
# Path to apple smc driver config path
FAN_PATH="/sys/devices/platform/applesmc.768"
# First fan macro
FAN_1="$FAN_PATH/fan1_"
# Second fan macro
FAN_2="$FAN_PATH/fan2_"
# Current fan speed macro
RPM_IN="input"
# Speed set for fan macro
RPM_OUT="output"
# Manual mode set for fan macro
MANUAL_OUT="manual"
# Safe rpm substructor for max speed
SAFE_RPM=200
# Fan 1 minimal RPM
FAN_1_MIN_RPM=$(<$FAN_1"min")
# Fan 1 maximal RPM
FAN_1_MAX_RPM=$(<$FAN_1"max")
FAN_1_MAX_RPM=$((FAN_1_MAX_RPM - SAFE_RPM))
# Fan 2 minimal RPM
FAN_2_MIN_RPM=$(<$FAN_1"min")
# Fan 2 maximal RPM
FAN_2_MAX_RPM=$(<$FAN_2"max")
FAN_2_MAX_RPM=$((FAN_2_MAX_RPM - SAFE_RPM))
# Prints sensor values to standard output
function print_sensors() {
# Current core temperature in thousands
CORE_TEMP=$(<$CORE_SENSOR)
# Current temperature in C
CORE_TEMP=$((CORE_TEMP / 1000))
# Fan 1 current RPM
FAN_1_RPM=$(<$FAN_1$RPM_IN)
# Fan 2 current RPM
FAN_2_RPM=$(<$FAN_2$RPM_IN)
# Print values
printf '%s\t%s%c\n' "Core temp:" "$CORE_TEMP" "c"
printf '%s\t\t%s%s\n' "Fan 1:" "$FAN_1_RPM" "rpm"
printf '%s\t\t%s%s\n' "Fan 2:" "$FAN_2_RPM" "rpm"
}
# Checks root rights for user running this script
function check_root() {
if [ $UID != 0 ]
then
# If user isn't root print message and exit with code 1
echo "Please, run this script with root to control fans."
# Write result to syslog
logger -t $SCRIPT_NAME "Can't execute with $1 without root."
exit 1
fi
}
# Checks if process is already running
function mutex() {
local file=$1 pid pids
exec 9>>"$file"
{ pids=$(fuser -f "$file"); } 2>&- 9>&-
for pid in $pids; do
[[ $pid = $$ ]] && continue
exec 9>&-
return 1 # Locked by a pid.
done
}
# Main condition sequence
if [ "$1" = "--on" ]
then
# If first argument is --on
# Call root cheking
check_root "$@"
# If script with this option is already running then exit
mutex /var/run/mac_fan_booster.lock || { echo "$SCRIPT_NAME is already running!" >&2; exit 1; }
# Write boosting enabled event to syslog
logger -t $SCRIPT_NAME "Boosting fans..."
# Set manual mode for Fan 1 and Fan 2
echo 1 > $FAN_1$MANUAL_OUT
echo 1 > $FAN_2$MANUAL_OUT
# Set speed for Fan 1 and Fan 2
echo $FAN_1_MAX_RPM > $FAN_1$RPM_OUT
echo $FAN_2_MAX_RPM > $FAN_2$RPM_OUT
# Call script itself with -v arg
"$0" -v
elif [ "$1" = "--off" ]
then
# If first argument is --off
# Call root checking
check_root "$@"
# Write fan speed control returned to system event to syslog
logger -t $SCRIPT_NAME "Boosting disabling. Returning fan speed control to system..."
# Return Fan 1 and Fan 2 speed control to system
echo 0 > $FAN_1$MANUAL_OUT
echo 0 > $FAN_2$MANUAL_OUT
# Kill all instances of script
pkill -9 -f "$0"
elif [ "$1" = "-v" ]
then
# If -v argument is provided
# Write status printing to terminal event to syslog
logger -t $SCRIPT_NAME "Printing status..."
# Clear screen, call print sensors with 0.3 sec pause in loop
while true;
do
clear
print_sensors
sleep 0.3
done
elif [ "$1" = "-h" ]
then
# If -h argument is provided
# Print help text
printf '%s\n' "Available arguments:"
printf '\t%s\t\t%c\t%s\n' "-h" "-" "prints this help."
printf '\t%s\t\t%c\t%s\n' "-v" "-" "prints sensor values to screen."
printf '\t%s\t\t%c\t%s\n' "--on" "-" "boosts fans. (required root)"
printf '\t%s\t\t%c\t%s\n' "--off" "-" "returns fan speed control to system. (required root)"
printf '\t%s\t%c\t%s\n' "--auto min max" "-" "automaticaly controls fans rpm between min and max core temp"
elif [[ "$1" = "--auto" && "$2" =~ ^[0-9]+$ && "$3" =~ ^[0-9]+$ ]]
then
# If --auto argument is provided
# Call root cheking
check_root "$@"
# If script with this option is already running then exit
mutex /var/run/mac_fan_booster.lock || { echo "$SCRIPT_NAME is already running!" >&2; exit 1; }
# Temperature after which cooling will be enabled
TRESHOLD_TEMP=$2
# Maximal temperature
MAX_TEMP=$3
if [ "$TRESHOLD_TEMP" -gt "$MAX_TEMP" ]
then
# If treshold temperature is greater than maximal temperature
echo "Minimal treshold temperature can't be greater than maximum core temperature!"
exit 1
elif [ "$TRESHOLD_TEMP" = 0 -o "$MAX_TEMP" = 0 ]
then
# If treshold temperature of maximal temperature are zeroes
echo "Minimal treshold or maximum core temperature can't be zero!"
exit 1
fi
# Write event to syslog
logger $SCRIPT_NAME "Working in auto mode. Treshold ($2) -> Max ($3)."
# Available temperature degrees for current temperature range
AVAIL_DEGREES=$((MAX_TEMP - TRESHOLD_TEMP))
# Available fan rpm to work with
FAN_1_AVAIL_RPM=$((FAN_1_MAX_RPM - FAN_1_MIN_RPM))
# Incrementing rpm step
FAN_1_RPM_STEP=$((FAN_1_AVAIL_RPM / AVAIL_DEGREES))
# Available fan rpm to work with
FAN_2_AVAIL_RPM=$((FAN_2_MAX_RPM - FAN_2_MIN_RPM))
# Incrementing rpm step
FAN_2_RPM_STEP=$((FAN_2_AVAIL_RPM / AVAIL_DEGREES))
# Set manual mode for Fan 1 and Fan 2
echo 1 > $FAN_1$MANUAL_OUT
echo 1 > $FAN_2$MANUAL_OUT
# Cooling loop
while true;
do
# Take current core temp
CURRENT_TEMP=$(<$CORE_SENSOR)
# Divide it by 1000 to get traditional degrees
CURRENT_TEMP=$((CURRENT_TEMP / 1000))
if [[ ! $CURRENT_TEMP < $TRESHOLD_TEMP ]]
then
# If current core temp rised greter than treshold
TEMP_OFFSET=$((MAX_TEMP - CURRENT_TEMP))
TEMP_STEP=$((AVAIL_DEGREES - TEMP_OFFSET))
FAN_1_RPM=$((FAN_1_RPM_STEP * TEMP_STEP + FAN_1_MIN_RPM))
FAN_2_RPM=$((FAN_2_RPM_STEP * TEMP_STEP + FAN_2_MIN_RPM))
# Set fans speed
echo $FAN_1_RPM > $FAN_1$RPM_OUT
echo $FAN_2_RPM > $FAN_2$RPM_OUT
elif [[ $CURRENT_TEMP < $TRESHOLD_TEMP ]]
then
# If current core temp is less than treshold
# Set minimal fans speed
echo $FAN_1_MIN_RPM > $FAN_1$RPM_OUT
echo $FAN_2_MIN_RPM > $FAN_2$RPM_OUT
fi
# Need to sleep cause to very fast rpm changing
sleep 1
done
else
# Else print that provided argument is unknown
echo "Unknown argument: $1!"
# Call script itself with -h arg
"$0" -h
fi