-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpuman-cli_v0.1.sh
299 lines (208 loc) · 9.64 KB
/
cpuman-cli_v0.1.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/bin/bash
# name : cpuman-cli
# desciption : manage cpu cores and powerstatus of cpu
# autor : speefak ( [email protected] )
# licence : (CC) BY-NC-SA
# version : 0.1
# notice :
# infosource : https://askubuntu.com/questions/1185826/how-do-i-disable-a-specific-cpu-core-at-boot
#
#------------------------------------------------------------------------------------------------------------
############################################################################################################
####################################### define global variables ########################################
############################################################################################################
#------------------------------------------------------------------------------------------------------------
RequiredPackets="bash sed awk"
Version=$(cat $0 | grep "# version" | head -n1 | awk -F ":" '{print $2}' | sed 's/ //g')
ScriptName=$(basename $0)
#------------------------------------------------------------------------------------------------------------
############################################################################################################
######################################## set vars from options ##########################################
############################################################################################################
#------------------------------------------------------------------------------------------------------------
OptionVarList="
ProcessingCPUCores;-c
ProcessingCPUStatus;-p
HelpDialog;-h
Monochrome;-m
ScriptInformation;-si
CheckForRequiredPackages;-cfrp
"
# set entered vars from optionvarlist
OptionAllocator=" " # for option seperator "=" use cut -d "="
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for InputOption in $(echo " $@" | sed -e 's/-[a-z]/\n\0/g' ) ; do # | sed 's/ -/\n-/g'
for VarNameVarValue in $OptionVarList ; do
VarName=$(echo "$VarNameVarValue" | cut -d ";" -f1)
VarValue=$(echo "$VarNameVarValue" | cut -d ";" -f2)
if [[ $InputOption == "$VarValue" ]]; then
eval $(echo "$VarName"='$InputOption') # if [[ -n Option1 ]]; then echo "Option1 set";fi
#eval $(echo "$VarName"="true")
elif [[ $(echo $InputOption | cut -d "$OptionAllocator" -f1) == "$VarValue" ]]; then
eval $(echo "$VarName"='$(echo $InputOption | cut -d "$OptionAllocator" -f 2-10)')
fi
done
done
IFS=$SAVEIFS
#------------------------------------------------------------------------------------------------------------
############################################################################################################
########################################### define functions ###########################################
############################################################################################################
#------------------------------------------------------------------------------------------------------------
load_color_codes () {
Black='\033[0;30m' && DGray='\033[1;30m'
LRed='\033[0;31m' && Red='\033[1;31m'
LGreen='\033[0;32m' && Green='\033[1;32m'
LYellow='\033[0;33m' && Yellow='\033[1;33m'
LBlue='\033[0;34m' && Blue='\033[1;34m'
LPurple='\033[0;35m' && Purple='\033[1;35m'
LCyan='\033[0;36m' && Cyan='\033[1;36m'
LLGrey='\033[0;37m' && White='\033[1;37m'
Reset='\033[0m'
# Use them to print in your required colours:
# printf "%s\n" "Text in ${Red}red${Reset}, white and ${Blue}blue${Reset}."
BG='\033[47m'
FG='\033[0;30m'
# reload colored global vars
for i in $(cat $0 | sed '/load_color_codes/q' | grep '${Reset}'); do
eval "$i"
done
}
#------------------------------------------------------------------------------------------------------------
usage() {
printf "\n"
printf " Usage: $(basename $0) <options> "
printf "\n"
printf " -c <1-X> => used cpu cores \n"
printf " -p => cpu powerstatus \n"
printf " -h => help dialog \n"
printf " -m => monochrome output \n"
printf " -si => show script information \n"
printf " -cfrp => check for required packets \n"
printf "\n${LRed} $1 ${Reset}\n"
printf "\n"
exit
}
#------------------------------------------------------------------------------------------------------------
script_information () {
printf "\n"
printf " Scriptname: $ScriptName\n"
printf " Version: $Version \n"
printf " Location: $(pwd)/$ScriptName\n"
printf " Filesize: $(ls -lh $0 | cut -d " " -f5)\n"
printf "\n"
exit 0
}
#------------------------------------------------------------------------------------------------------------
check_for_required_packages () {
InstalledPacketList=$(dpkg -l | grep ii)
for Packet in $RequiredPackets ; do
if [[ -z $(grep -w $Packet <<< $InstalledPacketList) ]]; then
MissingPackets="$MissingPackets $Packet"
fi
done
# print status message / install dialog
if [[ -n $MissingPackets ]]; then
printf "missing packets: ${LRed} $MissingPackets ${Reset} \n"
read -e -p "install required packets ? (Y/N) " -i "Y" InstallMissingPackets
if [[ $InstallMissingPackets == [Yy] ]]; then
# install software packets
sudo apt update
sudo apt install -y $MissingPackets
if [[ ! $? == 0 ]]; then
exit
fi
else
printf "programm error: ${LRed} missing packets : $MissingPackets ${Reset} \n"
exit 1
fi
else
printf "${LGreen} all required packets detected ${Reset}\n"
fi
}
#------------------------------------------------------------------------------------------------------------
core_manager () {
CPUCoreList=$(grep -E 'processor|core id' /proc/cpuinfo | sed 's/processor/\nprocessor/g')
CPUCoreCount=$(($(grep "core id" /proc/cpuinfo | tail -n1 | awk -F ": " '{print $2}')+1))
ProcessorCount=$(($(grep "processor" /proc/cpuinfo | tail -n1 | awk -F ": " '{print $2}')+1))
# check for hyperthreading
CPUHyperThread=disabled
if [[ ! $CPUCoreCount == $ProcessorCount ]]; then
CPUHyperThread=enabled
fi
# print machine specs
printf " detected CPU cores: $CPUCoreCount \n"
printf " detected processors: $ProcessorCount \n"
printf " CPU hyperthreading: $CPUHyperThread \n"
# check for correct cpu core input
if [[ $ProcessingCPUCores -gt $CPUCoreCount ]]; then
usage " entered cores: $ProcessingCPUCores \n available cores: $CPUCoreCount \n"
fi
#
#echo "$CPUCoreList" | grep -B1 "core id.*$1"
echo
#echo "A$CPUCoreCount"
#echo "B$ProcessingCPUCores"
#echo "$CPUCoreList"
# processing cpu cores
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for i in $(seq 0 1 $(($ProcessingCPUCores-1)) ); do
printf " disable core $(($i+1)) "
printf " processor $CPUCoreList" | grep -B1 "core id.*$i" | awk -F ": " '{printf $2}'
printf "\n\n"
done
IFS=$SAVEIFS
#grep -a1 "core id.*3" $CPUCoreList | grep processor
exit
echo core
grep -E 'processor|core id' /proc/cpuinfo
for x in /sys/devices/system/cpu/cpu[1-9]*/online; do echo 1 >"$x"; done
for file in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do echo "powersave" > $file; done
}
#------------------------------------------------------------------------------------------------------------
power_manager () {
echo power
}
#------------------------------------------------------------------------------------------------------------
############################################################################################################
############################################# start script #############################################
############################################################################################################
#------------------------------------------------------------------------------------------------------------
# check for cronjob execution and cronjob options
CronExecution=
if [ -z $(grep "/" <<< "$(tty)") ]; then
CronExecution=true
Monochrome=true
fi
#------------------------------------------------------------------------------------------------------------
# check for monochrome output
if [[ -z $Monochrome ]]; then
load_color_codes
fi
#------------------------------------------------------------------------------------------------------------
# check help dialog
if [[ -n $HelpDialog ]] || [[ -z $1 ]]; then usage "help dialog" ; fi
#------------------------------------------------------------------------------------------------------------
# check for script information
if [[ -n $ScriptInformation ]]; then script_information ; fi
#------------------------------------------------------------------------------------------------------------
# check for root permission
if [ "$(whoami)" = "root" ]; then echo "";else echo "Are You Root ?";exit 1;fi
#------------------------------------------------------------------------------------------------------------
# check for required package
if [[ -n $CheckForRequiredPackages ]]; then check_for_required_packages; fi
#------------------------------------------------------------------------------------------------------------
# manage cpu cores
if [[ -n $ProcessingCPUCores ]]; then core_manager; fi
#------------------------------------------------------------------------------------------------------------
# manage cpu powerstatus
if [[ -n $ProcessingCPUStatus ]]; then power_manager; fi
#------------------------------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------------------------------
exit 0
#infosource
#grep -E 'processor|core id' /proc/cpuinfo
#for x in /sys/devices/system/cpu/cpu[1-9]*/online; do echo 1 >"$x"; done
#for file in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do echo "powersave" > $file; done