-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmem_cpu_check_kill.tex
263 lines (248 loc) · 7.42 KB
/
mem_cpu_check_kill.tex
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
\documentclass[12pt,a4paper]{article}
\title{Script to check memory usage, cpu usage and to kill processes if they use more than the specified value.}
\author{Jilvin Jacob, Amal S Raj, Anish R}
\date{February 23 2017}
\begin{document}
\maketitle
\begin{center}
https://github.com/jilvin/mem\_cpu\_check\_kill
\end{center}
\newpage
\section{Introduction}
What we are up to?
Our task was to write a shell script that checks on memory usage and cpu usage of various processes and to kill them if they use more than a specified value. Moreover we had to set it start at system startup.
\newpage
\section{Script Declaration}
We are writing a shell script. So it is advised to add first line as:\\
\#!/bin/sh
\newline
\begin{flushleft}
Script now:
\newline
\#!/bin/sh
\end{flushleft}
\newpage
\section{Obtaining information from system.}
We need to obtain information on currenly working processes from the system. Here, we will be using the package 'ps'.\\So we will be adding line as:\\
ps
\newline
\begin{flushleft}
Script now:
\newline
\#!/bin/sh\\
ps
\end{flushleft}
\newpage
\section{ps need flags}
Simply using ps won't return what we actually need. We need pmem and pcpu values of all processes.\\So we will be rewriting line as:\\
ps -e -o pmem,pcpu,pid,user,comm
\newline
\begin{flushleft}
Script now:
\newline
\#!/bin/sh\\
ps -e -o pmem,pcpu,pid,user,comm\\
\end{flushleft}
\newpage
\section{ps need "more" flags}
When we use ps -e -o pmem,pcpu,pid,user,comm; We actually will have to skip the first return line before processing. To skip the titles, it is better to add an '=' after each format name.\\So we will be rewriting line as:\\
ps -e -o pmem=,pcpu=,pid=,user=,comm=
\newline
\begin{flushleft}
Script now:
\newline
\#!/bin/sh\\
ps -e -o pmem=,pcpu=,pid=,user=,comm=\\
\end{flushleft}
\newpage
\section{Advantage of sorting}
Here, we are to kill processes that use more resources than a specified value, so it is better to get a sorted return from ps.\\So we will be rewriting line as:\\
ps -e -o pmem=,pcpu=,pid=,user=,comm= --sort=-pmem\\
The - symbol indicates that sorting must be in descending order with respect to pmem values.
\newline
\begin{flushleft}
Script now:
\newline
\#!/bin/sh\\
ps -e -o pmem=,pcpu=,pid=,user=,comm= --sort=-pmem\\
\end{flushleft}
\newpage
\section{Start reading values.}
We haven't actually read the values into our script/process.So we will be adding a '|' operator and a while loop to read each line of return.\\
ps -e -o pmem=,pcpu=,pid=,user=,comm= --sort=-pmem |\\
while read size cpu pid user comm\\
do\\
done\\
Here size, cpu, pid, user, comm are variables of the script. ie values from ps will be saved to these variables.
\newline
\begin{flushleft}
Script now:
\newline
\#!/bin/sh\\
ps -e -o pmem=,pcpu=,pid=,user=,comm= --sort=-pmem |\\
while read size cpu pid user comm\\
do\\
done\\
\end{flushleft}
\newpage
\section{Declaring check values.}
We have to declare the values to be checked for at the beginning of the script.So we will be adding the lines:\\
check\_mem\_val=10.0\\
check\_cpu\_val=10.0\\
We are using float values because the return from ps will be in percentage for pmem and pcpu.
\newline
\begin{flushleft}
Script now:
\newline
\#!/bin/sh\\
check\_mem\_val=10.0\\
check\_cpu\_val=10.0\\
ps -e -o pmem=,pcpu=,pid=,user=,comm= --sort=-pmem |\\
while read size cpu pid user comm\\
do\\
done\\
\end{flushleft}
\newpage
\section{Comparing values.}
We have to compare the values(value obtained, value specified) in order to kill processes using more resources than specified. So we will be adding the lines within the while loop:\\
kill\_mem=\$( echo "\$size>\$check\_mem\_val" | bc )\\
kill\_cpu=\$( echo "\$cpu>\$check\_cpu\_val" | bc )\\
if [ "\$kill\_mem" = "1" ]\\
then\\
kill \$pid \# \$size \$user \$comm\\
elif [ "\$kill\_cpu" = "1" ]\\
then\\
kill \$pid \# \$size \$user \$comm\\
else\\
continue\\
fi\\
bc is a package that is used for comparisons of float values as shell isn't capable of doing that.\\ bc returns '1' if it the condition is satisfied.
The kill command is used for killing a process.
\newpage
\begin{flushleft}
Script now:
\newline
\#!/bin/sh\\
check\_mem\_val=10.0\\
check\_cpu\_val=10.0\\
ps -e -o pmem=,pcpu=,pid=,user=,comm= --sort=-pmem |\\
while read size cpu pid user comm\\
do\\
kill\_mem=0\\
kill\_cpu=0\\
kill\_mem=\$( echo "\$size>\$check\_mem\_val | bc" )\\
kill\_cpu=\$( echo "\$cpu>\$check\_cpu\_val" | bc )\\
if [ "\$kill\_mem" = "1" ]\\
then\\
kill \$pid \# \$size \$user \$comm\\
elif [ "\$kill\_cpu" = "1" ]\\
then\\
kill \$pid \# \$size \$user \$comm\\
else\\
continue\\
fi\\
done\\
\end{flushleft}
\newpage
\section{Comparing user.}
It is better to kill processes owned by the user not the root. So we will be adding an outer if structure.\\
\begin{flushleft}
Script now:
\newline
\#!/bin/sh\\
check\_mem\_val=10.0\\
check\_cpu\_val=10.0\\
ps -e -o pmem=,pcpu=,pid=,user=,comm= --sort=-pmem |\\
while read size cpu pid user comm\\
do\\
kill\_mem=0\\
kill\_cpu=0\\
if [ "\$user" = "golden-+" ]\\
then\\
kill\_mem=\$( echo "\$size>\$check\_mem\_val | bc" )\\
kill\_cpu=\$( echo "\$cpu>\$check\_cpu\_val" | bc )\\
if [ "\$kill\_mem" = "1" ]\\
then\\
kill \$pid \# \$size \$user \$comm\\
elif [ "\$kill\_cpu" = "1" ]\\
then\\
kill \$pid \# \$size \$user \$comm\\
else\\
continue\\
fi\\
fi\\
done\\
\end{flushleft}
\newpage
\section{Infinite loop.}
Also, we need to set an outer infinite loop so that the program is repeated all the time the system is up.\\
\begin{flushleft}
Script now:
\newline
\#!/bin/sh\\
check\_mem\_val=10.0\\
check\_cpu\_val=10.0\\
while(true)\\
do\\
ps -e -o pmem=,pcpu=,pid=,user=,comm= --sort=-pmem |\\
while read size cpu pid user comm\\
do\\
kill\_mem=0\\
kill\_cpu=0\\
if [ "\$user" = "golden-+" ]\\
then\\
kill\_mem=\$( echo "\$size>\$check\_mem\_val | bc" )\\
kill\_cpu=\$( echo "\$cpu>\$check\_cpu\_val" | bc )\\
if [ "\$kill\_mem" = "1" ]\\
then\\
kill \$pid \# \$size \$user \$comm\\
elif [ "\$kill\_cpu" = "1" ]\\
then\\
kill \$pid \# \$size \$user \$comm\\
else\\
continue\\
fi\\
fi\\
done\\
done\\
\end{flushleft}
\newpage
\section{Control process usage.}
We created a process to control cpu usage. We don't want it to use more of the cpu. So we will be sleeping/pausing the outer loop for 1s.\\We will be using sleep command.\\
\begin{flushleft}
Script now:
\newline
\#!/bin/sh\\
check\_mem\_val=10.0\\
check\_cpu\_val=10.0\\
while(true)\\
do\\
ps -e -o pmem=,pcpu=,pid=,user=,comm= --sort=-pmem |\\
while read size cpu pid user comm\\
do\\
kill\_mem=0\\
kill\_cpu=0\\
if [ "\$user" = "golden-+" ]\\
then\\
kill\_mem=\$( echo "\$size>\$check\_mem\_val | bc" )\\
kill\_cpu=\$( echo "\$cpu>\$check\_cpu\_val" | bc )\\
if [ "\$kill\_mem" = "1" ]\\
then\\
kill \$pid \# \$size \$user \$comm\\
elif [ "\$kill\_cpu" = "1" ]\\
then\\
kill \$pid \# \$size \$user \$comm\\
else\\
continue\\
fi\\
fi\\
done\\
sleep 1\\
done\\
\end{flushleft}
\newpage
\section{Adding the script to startup.(optional)}
To make the script run on startup on Debian/Ubuntu do the following steps.\\\\1) Copy the script to /etc/init.d directory\\\\2) sudo update-rc.d mem\_cpu\_check\_kill.sh defaults\\\\
To remove the script from running on startup, do:\\\\
sudo update-rc.d -f mem\_cpu\_check\_kill.sh remove\\\\
\end{document}