Raspberry Pi_Eng_20.3.4 Automatic Execution by Run Level at Booting


Published Book on Amazon


All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 1
All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 2


출판된 한글판 도서


최신 라즈베리파이(Raspberry

Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (상)

최신 라즈베리파이(Raspberry

Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (하)


Original Book Contents


20.3.4  Automatic Execution by Run Level at Booting

 

20.3.4.1    Principles of Registering Init Script

 

After you have created an init script and saved it in "/etc/init.d", you need to register it on the system so that it runs automatically according to the run level. If we systematically look into registering on the system, you can see that it is creating a link to the original script file. In other words, running the link will run the original script file.

 

The scripts that are automatically executed according to the run level in the system are registered under "/etc/rc0.d/" ~ "/etc/rc6.d/" directory. In other words, the script you want to run depending on the run level is registered by creating and placing a link under each directory.

 

When creating a link, there are a few naming rules of the link. The script that you want to start at a certain run level should be named in the format of "Snn" by combining "S" and number, and the script you want to stop at a certain run level should be named in the format of "Knn" by combining "K" and number. When executing the script on the system, the script starting with "S" executes the start operation and the script starting with "K" executes stop operation. The number "nn" attached to the link name is used for ordering in the folder. For example, for script "motion", link name "S01motion" is used for start, and link name "K01motion" is used for stop.

 

The basic scheme in which the script is executed automatically according to the run level in the booting sequence is as follows.

    /etc/init.d/rc N                    à/etc/init.d/rc              

à/etc/rcN.d/KnnAppl link      à/etc/init.d/Appl stop    à stop Appl

    /etc/init.d/rc N                    à/etc/init.d/rc   

à/etc/rcN.d/SnnAppl link      à/etc/init.d/Appl start    àstart Appl      


 

20.3.4.2    "update-rc.d" Command Automatic Registering

 

You can register init scripts manually by creating link and registering them one by one, but you can use "update-rc.d" command to do all the work at once.

 

[Command Format]

update-rc.d   script  [option]

 

[Command Overview]

    This automatically registers the init script at once.

    User privilege           -- super user.

                                  

[Detail Description]

You usually register the init script with the defaults option. Then, for run levels "2", "3", "4", and "5", it register "SnnScript" to start the corresponding init script, and register "KnnScript" to stop init script for run level "0", "1", and "6".

 

[Main option]

defaults

 

It automatically registers at run level.

If defaults is used then update-rc.d will make links to start the service in runlevels 2345 and to stop the service in runlevels 016.

start/stop

 

Instead of defaults one can give one or more sets of arguments specifying particular runlevels in which to start or stop the service. Each of these sets of arguments starts with the keyword start or stop and a sequence number NN, followed by one or more runlevel numbers. The set is terminated by a solitary full stop character. When explicit specification, rather than defaults, is used there will usually be one start and one stop set. If different sequence codes are required in different runlevels then several start sets or several stop sets may be specified.  If this is done and the same runlevel is named in multiple sets then only the last one counts. Therefore it is not possible to create multiple start or multiple stop links for a service in a single runlevel directory.

remove

 

When invoked with the remove option, update-rc.d removes any links in the /etc/rcrunlevel.d directories to the script /etc/init.d/name. The script must have been deleted already. If the script is still present then update-rc.d aborts with an error message.

disable/enable

 

When run with the disable [ S|2|3|4|5 ] options, update-rc.d modifies existing runlevel links for the script /etc/init.d/name by renaming start links to stop links with a sequence number equal to the difference of 100 minus the original sequence number.

 

When run with the enable [ S|2|3|4|5 ] options, update-rc.d modifies existing runlevel links for the script /etc/init.d/name by renaming stop links to start links with a sequence number equal to the positive difference of current sequence number minus 100, thus returning to the original sequence number that the script had been installed with before disabling it.

 


 

20.3.4.3    Starting and Stopping Init Script

 

If the init script is registered for a specific application, the daemon for that application can be started or stopped immediately by using command.

 

The format to use "service" command to immediately start/stop the daemon for a particular application is as follows.

 

[Command Format]

sudo  service   <application-init-script>    [ start / stop ]

 

The format to use the init script file directly to immediately stop the daemon for a specific application is as follows.

 

[Command Format]

sudo  /etc/init.d/< application-init-script >  [ start / stop ]