source: chapter07/functions.xml@ d10f4b4

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

Fixed indentations

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