source: chapter07/functions.xml@ a9a3db0

10.0 10.0-rc1 10.1 10.1-rc1 11.0 11.0-rc1 11.0-rc2 11.0-rc3 11.1 11.1-rc1 11.2 11.2-rc1 11.3 11.3-rc1 12.0 12.0-rc1 12.1 12.1-rc1 6.0 6.1 6.1.1 6.3 6.4 6.5 6.6 6.7 6.8 7.0 7.1 7.2 7.3 7.4 7.5 7.5-systemd 7.6 7.6-systemd 7.7 7.7-systemd 7.8 7.8-systemd 7.9 7.9-systemd 8.0 8.1 8.2 8.3 8.4 9.0 9.1 arm bdubbs/gcc13 ml-11.0 multilib renodr/libudev-from-systemd s6-init trunk v3_0 v3_1 v3_2 v3_3 v4_0 v4_1 v5_0 v5_1 v5_1_1 xry111/arm64 xry111/arm64-12.0 xry111/clfs-ng xry111/lfs-next xry111/loongarch xry111/loongarch-12.0 xry111/loongarch-12.1 xry111/mips64el xry111/pip3 xry111/rust-wip-20221008 xry111/update-glibc
Last change on this file since a9a3db0 was 3229c6b, checked in by Gerard Beekmans <gerard@…>, 23 years ago

Changed literallayout's to screen's

git-svn-id: http://svn.linuxfromscratch.org/LFS/trunk/BOOK@771 4aa44e1e-78dd-0310-a6d2-fbcd4c07a689

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