source: chapter07/functions.xml@ 713cffb

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 713cffb was a2f729c2, checked in by Gerard Beekmans <gerard@…>, 23 years ago

changed $* into "$@" in the functions script

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