source: chapter07/functions.sgml@ eb72c3bc

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

Initial commit - LFS 2.4.4 files

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

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