source: chapter07/functions.xml@ 4d994af4

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

Spell Checks

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

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