1 | <sect1 id="ch07-functions">
|
---|
2 | <title>Creating the functions script</title>
|
---|
3 |
|
---|
4 | <para>
|
---|
5 | A new file <filename>/etc/init.d/functions</filename> is created containing
|
---|
6 | the following:
|
---|
7 | </para>
|
---|
8 |
|
---|
9 | <literallayout>
|
---|
10 | <userinput>cat > functions << "EOF"</userinput>
|
---|
11 | #!/bin/sh
|
---|
12 | # Begin /etc/init.d/functions
|
---|
13 |
|
---|
14 | #
|
---|
15 | # Set a few variables that influence the text that's printed on the
|
---|
16 | # screen. The SET_COL variable starts the text in column number 70 (as
|
---|
17 | # defined by the COL variable). NORMAL prints text in normal mode.
|
---|
18 | # SUCCESS prints text in a green colour and FAILURE prints text in a red
|
---|
19 | # colour
|
---|
20 | #
|
---|
21 |
|
---|
22 | COL=70
|
---|
23 | SET_COL="echo -en \\033[${COL}G"
|
---|
24 | NORMAL="echo -en \\033[0;39m"
|
---|
25 | SUCCESS="echo -en \\033[1;32m"
|
---|
26 | FAILURE="echo -en \\033[1;31m"
|
---|
27 |
|
---|
28 | #
|
---|
29 | # The evaluate_retval function evaluates the return value of the process
|
---|
30 | # that was run just before this function was called. If the return value
|
---|
31 | # was 0, indicating success, the print_status function is called with
|
---|
32 | # the 'success' parameter. Otherwise the print_status function is called
|
---|
33 | # with the failure parameter.
|
---|
34 | #
|
---|
35 |
|
---|
36 | evaluate_retval()
|
---|
37 | {
|
---|
38 | if [ $? = 0 ]
|
---|
39 | then
|
---|
40 | print_status success
|
---|
41 | else
|
---|
42 | print_status failure
|
---|
43 | fi
|
---|
44 | }
|
---|
45 |
|
---|
46 | #
|
---|
47 | # The print_status prints [ OK ] or [FAILED] to the screen. OK appears
|
---|
48 | # in the colour defined by the SUCCESS variable and FAILED appears in
|
---|
49 | # the colour defined by the FAILURE variable. Both are printed starting
|
---|
50 | # in the column defined by the COL variable.
|
---|
51 | #
|
---|
52 |
|
---|
53 | print_status()
|
---|
54 | {
|
---|
55 |
|
---|
56 | #
|
---|
57 | # If no parameters are given to the print_status function, print usage
|
---|
58 | # information.
|
---|
59 | #
|
---|
60 |
|
---|
61 | if [ $# = 0 ]
|
---|
62 | then
|
---|
63 | echo "Usage: print_status {success|failure}"
|
---|
64 | return 1
|
---|
65 | fi
|
---|
66 |
|
---|
67 | case "$1" in
|
---|
68 | success)
|
---|
69 | $SET_COL
|
---|
70 | echo -n "[ "
|
---|
71 | $SUCCESS
|
---|
72 | echo -n "OK"
|
---|
73 | $NORMAL
|
---|
74 | echo " ]"
|
---|
75 | ;;
|
---|
76 | failure)
|
---|
77 | $SET_COL
|
---|
78 | echo -n "["
|
---|
79 | $FAILURE
|
---|
80 | echo -n "FAILED"
|
---|
81 | $NORMAL
|
---|
82 | echo "]"
|
---|
83 | ;;
|
---|
84 | esac
|
---|
85 |
|
---|
86 | }
|
---|
87 |
|
---|
88 | #
|
---|
89 | # The loadproc function starts a process (often a daemon) with
|
---|
90 | # proper error checking
|
---|
91 | #
|
---|
92 |
|
---|
93 | loadproc()
|
---|
94 | {
|
---|
95 |
|
---|
96 | #
|
---|
97 | # If no parameters are given to the print_status function, print usage
|
---|
98 | # information.
|
---|
99 | #
|
---|
100 |
|
---|
101 | if [ $# = 0 ]
|
---|
102 | then
|
---|
103 | echo "Usage: loadproc {program}"
|
---|
104 | exit 1
|
---|
105 | fi
|
---|
106 | #
|
---|
107 | # Find the basename of the first parameter (the daemon's name without
|
---|
108 | # the path
|
---|
109 | # that was provided so /usr/sbin/syslogd becomes plain 'syslogd' after
|
---|
110 | # basename ran)
|
---|
111 | #
|
---|
112 |
|
---|
113 | base=$(/usr/bin/basename $1)
|
---|
114 | #
|
---|
115 | # the pidlist variable will contains the output of the pidof command.
|
---|
116 | # pidof will try to find the PID's that belong to a certain string;
|
---|
117 | # $base in this case
|
---|
118 | #
|
---|
119 |
|
---|
120 | pidlist=$(/bin/pidof -o $$ -o $PPID -o %PPID -x $base)
|
---|
121 |
|
---|
122 | pid=""
|
---|
123 |
|
---|
124 | for apid in $pidlist
|
---|
125 | do
|
---|
126 | if [ -d /proc/$apid ]
|
---|
127 | then
|
---|
128 | pid="$pid $apid"
|
---|
129 | fi
|
---|
130 | done
|
---|
131 | #
|
---|
132 | # If the $pid variable contains anything (from the previous for loop) it
|
---|
133 | # means the daemon is already running
|
---|
134 | #
|
---|
135 |
|
---|
136 | if [ ! -n "$pid" ]
|
---|
137 | then
|
---|
138 | #
|
---|
139 | # Empty $pid variable means it's not running, so we run $* (all
|
---|
140 | # parameters giving to this function from the script) and then check the
|
---|
141 | # return value
|
---|
142 | #
|
---|
143 | $*
|
---|
144 | evaluate_retval
|
---|
145 | else
|
---|
146 | #
|
---|
147 | # The variable $pid was not empty, meaning it was already running. We
|
---|
148 | # print [FAILED] now
|
---|
149 | #
|
---|
150 | print_status failure
|
---|
151 | fi
|
---|
152 |
|
---|
153 | }
|
---|
154 |
|
---|
155 | #
|
---|
156 | # The killproc function kills a process with proper error checking
|
---|
157 | #
|
---|
158 |
|
---|
159 | killproc()
|
---|
160 | {
|
---|
161 |
|
---|
162 | #
|
---|
163 | # If no parameters are given to the print_status function, print usage
|
---|
164 | # information.
|
---|
165 | #
|
---|
166 |
|
---|
167 | if [ $# = 0 ]
|
---|
168 | then
|
---|
169 | echo "Usage: killproc {program} [signal]"
|
---|
170 | exit 1
|
---|
171 | fi
|
---|
172 |
|
---|
173 | #
|
---|
174 | # Find the basename of the first parameter (the daemon's name without
|
---|
175 | # the path
|
---|
176 | # that was provided so /usr/sbin/syslogd becomes plain 'syslogd' after
|
---|
177 | # basename ran)
|
---|
178 | #
|
---|
179 |
|
---|
180 | base=$(/usr/bin/basename $1)
|
---|
181 |
|
---|
182 | #
|
---|
183 | # Check if we gave a signal to kill the process with (like -HUP, -TERM,
|
---|
184 | # -KILL, etc) to this function (the second parameter). If no second
|
---|
185 | # parameter was provided set the nolevel variable. Else set the
|
---|
186 | # killlevel variable to the value of $2 (the second parameter)
|
---|
187 | #
|
---|
188 |
|
---|
189 | if [ "$2" != "" ]
|
---|
190 | then
|
---|
191 | killlevel=-$2
|
---|
192 | else
|
---|
193 | nolevel=1
|
---|
194 | fi
|
---|
195 |
|
---|
196 | #
|
---|
197 | # the pidlist variable will contains the output of the pidof command.
|
---|
198 | # pidof will try to find the PID's that belong to a certain string;
|
---|
199 | # $base in this case
|
---|
200 | #
|
---|
201 |
|
---|
202 | pidlist=$(/bin/pidof -o $$ -o $PPID -o %PPID -x $base)
|
---|
203 |
|
---|
204 | pid=""
|
---|
205 |
|
---|
206 | for apid in $pidlist
|
---|
207 | do
|
---|
208 | if [ -d /proc/$apid ]
|
---|
209 | then
|
---|
210 | pid="$pid $apid"
|
---|
211 | fi
|
---|
212 | done
|
---|
213 |
|
---|
214 | #
|
---|
215 | # If $pid contains something from the previous for loop it means one or
|
---|
216 | # more PID's were found that belongs to the processes to be killed
|
---|
217 | #
|
---|
218 | if [ -n "$pid" ]
|
---|
219 | then
|
---|
220 | #
|
---|
221 | # If no kill level was specified we'll try -TERM first and then sleep
|
---|
222 | # for 2 seconds to allow the kill to be completed
|
---|
223 | #
|
---|
224 | if [ "$nolevel" = 1 ]
|
---|
225 | then
|
---|
226 | /bin/kill -TERM $pid
|
---|
227 | #
|
---|
228 | # If after -TERM the PID still exists we'll wait 2 seconds before
|
---|
229 | # trying to kill it with -KILL. If the PID still exist after that, wait
|
---|
230 | # two more seconds. If the PIDs still exist by then it's safe to assume
|
---|
231 | # that we cannot kill these PIDs.
|
---|
232 | #
|
---|
233 |
|
---|
234 | if /bin/ps h $pid >/dev/null 2>&1
|
---|
235 | then
|
---|
236 | /usr/bin/sleep 2
|
---|
237 | if /bin/ps h $pid > /dev/null 2>&1
|
---|
238 | then
|
---|
239 | /bin/kill -KILL $pid
|
---|
240 | if /bin/ps h $pid > /dev/null 2>&1
|
---|
241 | then
|
---|
242 | /usr/bin/sleep 2
|
---|
243 | fi
|
---|
244 | fi
|
---|
245 | fi
|
---|
246 | /bin/ps h $pid >/dev/null 2>&1
|
---|
247 | if [ $? = 0 ]
|
---|
248 | then
|
---|
249 | #
|
---|
250 | # If after the -KILL it still exists it can't be killed for some reason
|
---|
251 | # and we'll print [FAILED]
|
---|
252 | #
|
---|
253 | print_status failure
|
---|
254 | else
|
---|
255 | #
|
---|
256 | # It was killed, remove possible stale PID file in /var/run and
|
---|
257 | # print [ OK ]
|
---|
258 | #
|
---|
259 | /bin/rm -f /var/run/$base.pid
|
---|
260 | print_status success
|
---|
261 | fi
|
---|
262 | else
|
---|
263 | #
|
---|
264 | # A kill level was provided. Kill with the provided kill level and wait
|
---|
265 | # for 2 seconds to allow the kill to be completed
|
---|
266 | #
|
---|
267 | /bin/kill $killlevel $pid
|
---|
268 | if /bin/ps h $pid > /dev/null 2>&1
|
---|
269 | then
|
---|
270 | /usr/bin/sleep 2
|
---|
271 | fi
|
---|
272 | /bin/ps h $pid >/dev/null 2>&1
|
---|
273 | if [ $? = 0 ]
|
---|
274 | then
|
---|
275 | #
|
---|
276 | # If ps' return value is 0 it means it ran ok which indicates that the
|
---|
277 | # PID still exists. This means the process wasn't killed properly with
|
---|
278 | # the signal provided. Print [FAILED]
|
---|
279 | #
|
---|
280 | print_status failure
|
---|
281 | else
|
---|
282 | #
|
---|
283 | # If the return value was 1 or higher it means the PID didn't exist
|
---|
284 | # anymore which means it was killed successfully. Remove possible stale
|
---|
285 | # PID file and print [ OK ]
|
---|
286 | #
|
---|
287 | /bin/rm -f /var/run/$base.pid
|
---|
288 | print_status success
|
---|
289 | fi
|
---|
290 | fi
|
---|
291 | else
|
---|
292 | #
|
---|
293 | # The PID didn't exist so we can't attempt to kill it. Print [FAILED]
|
---|
294 | #
|
---|
295 | print_status failure
|
---|
296 | fi
|
---|
297 | }
|
---|
298 |
|
---|
299 | #
|
---|
300 | # The reloadproc functions sends a signal to a daemon telling it to
|
---|
301 | # reload it's configuration file. This is almost identical to the
|
---|
302 | # killproc function with the exception that it won't try to kill it with
|
---|
303 | # a -KILL signal (aka -9)
|
---|
304 | #
|
---|
305 |
|
---|
306 | reloadproc()
|
---|
307 | {
|
---|
308 |
|
---|
309 | #
|
---|
310 | # If no parameters are given to the print_status function, print usage
|
---|
311 | # information.
|
---|
312 | #
|
---|
313 |
|
---|
314 | if [ $# = 0 ]
|
---|
315 | then
|
---|
316 | echo "Usage: reloadproc {program} [signal]"
|
---|
317 | exit 1
|
---|
318 | fi
|
---|
319 |
|
---|
320 | #
|
---|
321 | # Find the basename of the first parameter (the daemon's name without
|
---|
322 | # the path that was provided so /usr/sbin/syslogd becomes plain 'syslogd'
|
---|
323 | # after basename ran)
|
---|
324 | #
|
---|
325 |
|
---|
326 | base=$(/usr/bin/basename $1)
|
---|
327 |
|
---|
328 | #
|
---|
329 | # Check if we gave a signal to send to the process (like -HUP)
|
---|
330 | # to this function (the second parameter). If no second
|
---|
331 | # parameter was provided set the nolevel variable. Else set the
|
---|
332 | # killlevel variable to the value of $2 (the second parameter)
|
---|
333 | #
|
---|
334 |
|
---|
335 |
|
---|
336 | if [ -n "$2" ]
|
---|
337 | then
|
---|
338 | killlevel=-$2
|
---|
339 | else
|
---|
340 | nolevel=1
|
---|
341 | fi
|
---|
342 |
|
---|
343 | #
|
---|
344 | # the pidlist variable will contains the output of the pidof command.
|
---|
345 | # pidof will try to find the PID's that belong to a certain string;
|
---|
346 | # $base in this case
|
---|
347 | #
|
---|
348 |
|
---|
349 | pidlist=$(/bin/pidof -o $$ -o $PPID -o %PPID -x $base)
|
---|
350 |
|
---|
351 | pid=""
|
---|
352 |
|
---|
353 | for apid in $pidlist
|
---|
354 | do
|
---|
355 | if [ -d /proc/$apid ]
|
---|
356 | then
|
---|
357 | pid="$pid $apid"
|
---|
358 | fi
|
---|
359 | done
|
---|
360 |
|
---|
361 | #
|
---|
362 | # If $pid contains something from the previous for loop it means one or
|
---|
363 | # more PID's were found that belongs to the processes to be reloaded
|
---|
364 | #
|
---|
365 |
|
---|
366 | if [ -n "$pid" ]
|
---|
367 | then
|
---|
368 |
|
---|
369 | #
|
---|
370 | # If nolevel was set we will use the default reload signal SIGHUP.
|
---|
371 | #
|
---|
372 |
|
---|
373 | if [ "$nolevel" = 1 ]
|
---|
374 | then
|
---|
375 | /bin/kill -SIGHUP $pid
|
---|
376 | evaluate_retval
|
---|
377 | else
|
---|
378 | #
|
---|
379 | # Else we will use the provided signal
|
---|
380 | #
|
---|
381 |
|
---|
382 | /bin/kill $killlevel $pid
|
---|
383 | evaluate_retval
|
---|
384 | fi
|
---|
385 | else
|
---|
386 | #
|
---|
387 | # If $pid is empty no PID's have been found that belong to the process
|
---|
388 | # and print [FAILED]
|
---|
389 | #
|
---|
390 |
|
---|
391 | print_status failure
|
---|
392 | fi
|
---|
393 | }
|
---|
394 |
|
---|
395 | #
|
---|
396 | # The statusproc function will try to find out if a process is running
|
---|
397 | # or not
|
---|
398 | #
|
---|
399 |
|
---|
400 | statusproc()
|
---|
401 | {
|
---|
402 |
|
---|
403 | #
|
---|
404 | # If no parameters are given to the print_status function, print usage
|
---|
405 | # information.
|
---|
406 | #
|
---|
407 |
|
---|
408 | if [ $# = 0 ]
|
---|
409 | then
|
---|
410 | echo "Usage: status {program}"
|
---|
411 | return 1
|
---|
412 | fi
|
---|
413 |
|
---|
414 | #
|
---|
415 | # $pid will contain a list of PID's that belong to a process
|
---|
416 | #
|
---|
417 |
|
---|
418 | pid=$(/bin/pidof -o $$ -o $PPID -o %PPID -x $1)
|
---|
419 | if [ -n "$pid" ]
|
---|
420 | then
|
---|
421 | #
|
---|
422 | # If $pid contains something, the process is running, print the contents
|
---|
423 | # of the $pid variable
|
---|
424 | #
|
---|
425 | echo "$1 running with Process ID $pid"
|
---|
426 | return 0
|
---|
427 | fi
|
---|
428 |
|
---|
429 | #
|
---|
430 | # If $pid doesn't contain it check if a PID file exists and inform the
|
---|
431 | # user about this stale file.
|
---|
432 | #
|
---|
433 |
|
---|
434 | if [ -f /var/run/$1.pid ]
|
---|
435 | then
|
---|
436 | pid=$(/usr/bin/head -1 /var/run/$1.pid)
|
---|
437 | if [ -n "$pid" ]
|
---|
438 | then
|
---|
439 | echo "$1 not running but /var/run/$1.pid exists"
|
---|
440 | return 1
|
---|
441 | fi
|
---|
442 | else
|
---|
443 | echo "$1 is not running"
|
---|
444 | fi
|
---|
445 |
|
---|
446 | }
|
---|
447 |
|
---|
448 | # End /etc/init.d/functions
|
---|
449 | <userinput>EOF</userinput>
|
---|
450 | </literallayout>
|
---|
451 |
|
---|
452 | </sect1>
|
---|
453 |
|
---|