Raspberry Pi_Eng_20.3.3 Creating Init Script


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.3  Creating Init Script

 

20.3.3.1    Meaning of Init Script

 

The script that is used when the system boots or shuts down is called "init script". All init scripts basically should be put under "/etc/init.d/" directory.

 

There are several types of init scripts as follows according to their usage:

    Script to process different tasks depending on run level

It can do various tasks required for system management.

 

    Script to process specific application daemon according to run level

It is usually to make the script name of these init scripts the same as the application daemon name. For example, for <TightVNC> program, you can create a script that runs the daemon at booting and this script is created with script name "/etc/init.d/tightvnc".

 

    Script to be processed in running normally regardless of the run level

It can do various tasks required for system management.

 


 

20.3.3.2    How to Create Init Script by Run Level

 

The init script is usually written in the following format. First is a part to explain, next is a part to declare a variable, and after that is the part to actually process.

 

#!/bin/bash

# Start/stop the testpgm_init daemon.

# script description

### BEGIN INIT INFO

# comment item:              comment detail

### END INIT INFO

 

#variable declaration and value assignment

NAME= testpgm

SCRIPTNAME=/etc/init.d/"$NAME"

 

# processing according to request

case "$1" in       start)

                                 execution statement

                                 ;;

                      stop)

                                 execution statement

                                 ;;

                      restart)

                                 execution statement

                                 ;;

                      reload|force-reload)

                                 execution statement

                                 ;;

                      *)

                                 execution statement

                                 ;;

esac

exit 0

 

The first parts are annotations. It contains descriptions about the nature of init scripts. The content here is a simple description, and it explains that it works the system in that way.

 

~ Skip

### BEGIN INIT INFO

à It display the beginning of a comment.

 

# Provides:                testpgm_init

àIt specifies the name of script or daemon.

 

# Required-Start:           $all

à It means that this script will be started after all other normal processes have been processed.

 

# Required-Stop:           $all

à It means that this script will be stopped after all other normal processes have been processed.

 

# Default-Start:            2 3 4 5 

à It means the run level at which the system starts this script. Of course, you need to register the script to do so.

 

# Default-Stop:            0 1 6

à It means the run level at which the system stop this script. Of course, you need to register the script to do so.

 

# Short-Description:       Short-Description for testpgm_init

à It is a brief description of this script.

 

# Description:               Full-Description for testpgm_init

à It is a detail description of this script

 

### END INIT INFO

à It means the end of comment.

~ Skip

 

The following definition is the part to declare the important variables to use in the script. Variables defined here are used only within this script, and are variables that are lost when the script is executed.

 

The final definition is the part that does the actual work. Here, each processing is defined according to the value of the parameter. When the script is executed in the booting sequence, it is processed by specifying "start", "stop", or "restart" as execution parameter. The init script will then check the parameter "$1" and perform an appropriate process according to the processing request.

 


 

20.3.3.3    Creating Init Script to Run Application Daemon

 

The init script linked with a specific application is used to start/stop a specific application during system booting/shutdown. That is, the init script is executed in the booting sequence, and the init script starts/stops a specific application. Actually, the processing for daemon is done in the init script. That is, the basic framework is as follows.

    /etc/rcN.d/SnnAppl link        

à/etc/init.d/Appl start          à/etc/init.d/Appl's start-daemon Appl      à start Appl

    /etc/rcN.d/KnnAppl link                                                                

à/etc/init.d/Appl stopt         à/etc/init.d/Appl's stop-daemon Appl       à stop Appl

 

So when you start/stop a particular application like this, you can use the following statement in the execution statement.

    command to start

    start_daemon -p $PIDFILE $DAEMON

    start-stop-daemon --start --pidfile $PIDFILE --exec $DAEMON

 

    command to stop

    stop_daemon -p $PIDFILE $DAEMON

    killproc -p $PIDFILE $DAEMON

    start-stop-daemon --stop --pidfile /$PIDFILE

 

The following is the general format of an init script that will start/stop a particular application.

 

#!/bin/bash

# Start/stop the testpgm_init daemon.

 

~ Skip

 

#variable declaration and value assignment

PATH=/bin:/usr/bin:/sbin:/usr/sbin

DESC=" testpgm daemon"

NAME= testpgm

DAEMON=/usr/bin/testpgm

PIDFILE=/var/run/testpgm.pid

SCRIPTNAME=/etc/init.d/"$NAME"

 

#Checking whether the executed program file exists

test -f $DAEMON || exit 0

 

#Processing according to request

case "$1" in

           start)

                 start statement

                 execution statement

                      ;;

           stop)

                      stop statement

                 execution statement

                      ;;

           restart)

                 stop statement

                 execution statement

                      start statement

                 execution statement

                      ;;

           reload|force-reload)

                 execution statement

                      ;;

           *)

                      execution statement

                      ;;

esac

exit 0

 

In some cases, if you want to do further works only when you confirm that start/stop statement has been processed normally, you execute the start-statement or stop-statement in the following format.

 

if ( start- statement  /  stop- statement ) ;  then

         execution statement

else

           execution statement

fi