Raspberry Pi_Kor_20.3.4 run level 연동 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  run level 연동 booting 자동 실행

 

20.3.4.1    init script 등록 원리

 

init script를 작성하여 "/etc/init.d"에 저장한 다음에는 해당 script run level에 따라 자동 실행되도록 시스템에 등록해야 한다. 시스템에 등록한다는 것을 시스템적으로 자세히 살펴보면 원본 script 파일에 대해서 link를 만드는 것이라는 것을 알수 있다. link를 실행하면 원본 script 파일이 실행되는 것이다

 

시스템에서 run level에 따라 자동 실행하는 script "/etc/rc0.d/" ~ "/etc/rc6.d/" directory 밑에 등록되어 있다. run level에 따라서 실행하고 싶은 script link를 만들어서 각각의 directory 밑에 넣어 두면 등록이 된다.

 

link를 만들 때 link 이름에 약간의 규칙이 있는데, 특정 run level에서 start하고자 하는 script "Snn"의 형태로 "S"와 번호를 붙여서 이름을 부여하고, stop하고자 하는 script "Knn"의 형태로 "K"와 번호를 붙여서 이름을 부여해야 한다. 시스템에서 해당 script를 실행할 때 "S"로 시작하는 script start 작업을 시켜주고, "K"로 시작하는 script에 대해서는 stop 작업을 지시한다. link 이름에 붙이는 번호 "nn"은 폴더 내에서 순서를 정할 목적으로 사용한다. 예를 들어 script "motion"에 대한 link 이름이 "S01motion"이면 start할 때 사용하는 것이며, "K01motion"이면 stop할 때 사용하는 것이다.

 

booting sequence에서 run level에 따라 script가 자동 실행되는 기본 체계는 다음과 같다.

    /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" 명령 - init script 자동 등록

 

init script는 수작업으로 link를 만들어서 하나씩 등록할 수도 있지만, "update-rc.d" 명령을 사용하면 한번에 모든 작업을 처리해 준다

 

[명령 형식]

update-rc.d   script  [option]

 

[명령 개요]

    init script를 한번에 자동으로 등록해 준다.

    user 권한    -- super user.

                                  

[상세 설명]

통상 defaults option을 사용하여 init script를 등록한다. 그러면 run level "2", "3", "4, "5"에 대해서는 해당 init script start하도록 "SnnScript"를 등록해 주고, run level "0", "1", "6"에 대해서는 init script stop하도록 "KnnScript"를 등록해 준다.

 

[주요 option]

defaults

 

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    init script의 시작과 종료

 

특정 application에 대해서 init script이 등록되어 있는 경우, 명령을 사용하면 해당 application에 대한 daemon을 즉시 실행하거나 또는 중지시킬 수 있다.

 

특정 application에 대한 daemon을 즉시 start/stop하기 위해서 "service" 명령을 사용하는 방법은 다음과 같다.

 

[명령 형식]

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

 

 

특정 application에 대한 daemon을 즉시 stop/start하기 위해서 init script를 직접 사용하는 방법은 다음과 같다.

 

[명령 형식]

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