source: chapter07/functions.xml@ ba71c74

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

New smart COL and WCOL settings

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

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