source: chapter07/functions.xml@ edc42bb

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 edc42bb was edc42bb, checked in by Mark Hymers <markh@…>, 23 years ago

Fix & symbol cock up (again...)

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

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