source: chapter07/rc.xml@ 53b5ccf

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 53b5ccf was 53b5ccf, checked in by Thomas Balu Walter <tw@…>, 23 years ago

You

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

  • Property mode set to 100644
File size: 6.8 KB
Line 
1<sect1 id="ch07-rc">
2<title>Creating the rc script</title>
3
4<para>
5The first main bootscript is the <filename>/etc/init.d/rc</filename> script.
6A new file <filename>/etc/init.d/rc</filename> is created containing the
7following: </para>
8
9<literallayout>
10
11<userinput>cat &gt; rc &lt;&lt; "EOF"</userinput>
12#!/bin/sh
13# Begin /etc/init.d/rc
14#
15# By Jason Pearce - jason.pearce@linux.org
16# Modified by Gerard Beekmans - gerard@linuxfromscratch.org
17# print_error_msg based on ideas by Simon Perreault -
18# nomis80@videotron.ca
19
20#
21# Include the functions declared in the /etc/init.d/functions file
22#
23
24source /etc/init.d/functions
25
26#
27# The print_error_msg function prints an error message when an unforseen
28# error occured that wasn't trapped for some reason by a evaluate_retval
29# call or error checking in different ways.
30
31print_error_msg()
32{
33
34 echo
35 $FAILURE
36 echo -n "You should not read this error message. It means "
37 echo "that an unforseen error "
38 echo -n "took place and subscript $i exited with "
39 echo "a return value "
40 echo -n "of $error_value for an unknown reason. If you're able "
41 echo "to trace this error down "
42 echo -n "to a bug in one of the files provided by this book, "
43 echo "please be so kind to "
44 echo -n "inform us at lfs-discuss@linuxfromscratch.org"
45 $NORMAL
46 echo
47 echo
48 echo "Press a key to continue..."
49 read
50}
51
52#
53# If you uncomment the debug variable below none of the scripts will be
54# executed, just the script name and parameters will be echo'ed to the
55# screen so you can see how the scripts are called by rc.
56#
57
58# Un-comment the following for debugging.
59# debug=echo
60
61#
62# Start script or program.
63#
64startup() {
65
66$debug $*
67
68}
69
70#
71# Ignore CTRL-C only in this shell, so we can interrupt subprocesses.
72#
73
74trap ":" INT QUIT TSTP
75
76#
77# Now find out what the current and what the previous runlevel are. The
78# $RUNLEVEL variable is set by init for all it's children. This script
79# runs as a child of init.
80#
81
82runlevel=$RUNLEVEL
83
84#
85# Get first argument. Set new runlevel to this argument. If no runlevel
86# was passed to this script we won't change runlevels.
87#
88
89[ "$1" != "" ] &amp;&amp; runlevel=$1
90if [ "$runlevel" = "" ]
91then
92 echo "Usage: $0 &lt;runlevel&gt;" &gt;&amp;2
93 exit 1
94fi
95
96#
97# The same goes for $PREVLEVEL (see above for $RUNLEVEL). previous will
98# be set to the previous run level. If $PREVLEVEL is not set it means
99# that there is no previous runlevel and we'll set previous to N.
100#
101
102previous=$PREVLEVEL
103[ "$previous" = "" ] &amp;&amp; previous=N
104
105export runlevel previous
106
107#
108# Is there an rc directory for the new runlevel?
109#
110
111if [ -d /etc/rc$runlevel.d ]
112
113then
114
115#
116# If so, first collect all the K* scripts in the new run level.
117#
118
119 if [ $previous != N ]
120 then
121 for i in /etc/rc$runlevel.d/K*
122 do
123 [ ! -f $i ] &amp;&amp; continue
124
125#
126# the suffix variable will contain the script name without the leading
127# Kxxx
128#
129
130 suffix=${i#/etc/rc$runlevel.d/K[0-9][0-9][0-9]}
131#
132# If there is a start script for this K script in the previous runlevel
133# determine what it's full path is
134#
135 previous_start=/etc/rc$previous.d/S[0-9][0-9][0-9]$suffix
136#
137# If there was no previous run level it could be that something was
138# started in rcS.d (sysinit level) so we'll determine the path for that
139# possibility as well.
140#
141
142 sysinit_start=/etc/rcS.d/S[0-9][0-9][0-9]$suffix
143
144#
145# Stop the service if there is a start script in the previous run level
146# or in the sysinit level. If previous_start or sysinit_start do not
147# exist the 'continue' command is run which causes the script to abort
148# this iteration of the for loop and continue with the next iteration.
149# This boils down to that it won't run the commands after the next two
150# lines and start over from the top of this for loop. See man bash for
151# more info on this.
152#
153
154 [ ! -f $previous_start ] &amp;&amp;
155 [ ! -f $sysinit_start ] &amp;&amp; continue
156
157#
158# If we found previous_start or sysinit_start, run the K script
159#
160
161 startup $i stop
162 error_value=$?
163#
164# If the return value of the script is not 0, something went wrong with
165# error checking inside the script. the print_error_msg function will be
166# called and the message plus the return value of the K script will be
167# printed to the screen
168
169#
170
171 if [ $error_value != 0 ]
172 then
173 print_error_msg
174 fi
175
176 done
177 fi
178
179#
180# Now run the START scripts for this runlevel.
181#
182
183 for i in /etc/rc$runlevel.d/S*
184 do
185 [ ! -f $i ] &amp;&amp; continue
186
187 if [ $previous != N ]
188 then
189#
190# Find start script in previous runlevel and stop script in this
191# runlevel.
192#
193
194 suffix=${i#/etc/rc$runlevel.d/S[0-9][0-9][0-9]}
195 stop=/etc/rc$runlevel.d/K[0-9][0-9][0-9]$suffix
196 previous_start=/etc/rc$previous.d/S[0-9][0-9][0-9]$suffix
197#
198# If there is a start script in the previous level and no stop script in
199# this level, we don't have to re-start the service; abort this
200# iteration and start the next one.
201#
202
203 [ -f $previous_start ] &amp;&amp; [ ! -f $stop ] &amp;&amp;
204 continue
205 fi
206
207 case "$runlevel" in
208 0|6)
209
210#
211# levels 0 and 6 are halt and reboot levels. We don't really start
212# anything here so we call with the 'stop' parameter
213#
214
215 startup $i stop
216 error_value=$?
217#
218# If the return value of the script is not 0, something went wrong with
219# error checking inside the script. the print_error_msg function will be
220# called and the message plus the return value of the K script will be
221# printed to the screen
222#
223
224 if [ $error_value != 0 ]
225 then
226 print_error_msg
227 fi
228 ;;
229 *)
230 startup $i start
231 error_value=$?
232#
233# If the return value of the script is not 0, something went wrong with
234# error checking inside the script. the print_error_msg function will be
235# called and the message plus the return value of the K script will be
236# printed to the screen
237#
238
239 if [ $error_value != 0 ]
240 then
241 print_error_msg
242 fi
243 ;;
244 esac
245 done
246fi
247
248# End /etc/init.d/rc
249<userinput>EOF</userinput>
250
251</literallayout>
252
253</sect1>
254
Note: See TracBrowser for help on using the repository browser.