source: chapter07/functions.xml@ 793c2f7

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

XML changes

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

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