Skip to content

Init Scripts

Calin Crisan edited this page May 5, 2022 · 11 revisions

Naming

Init scripts are simple shell scripts that live in /etc/init.d. There's no dependency management, they are just run in alphabetical order. Naming of these scripts is therefore, important.

The scripts have the following naming convention:

S<XY><service_name>

S is a fixed letter, XY is a number of two digits (from 00 to 99) that dictates the order and service_name is the name given to the service. For example, S35wifi will start the WiFi service before S43firewall will start the firewall.

Internal Commands

Init scripts work with a command line argument, which represents the internal command supplied to the script. The following commands are supported by most scripts:

  • start - supported by all init scripts, starts the corresponding service
  • stop - supported by all init scripts, stops the corresponding service
  • restart - supported by all init scripts, internally runs a stop followed by a start
  • reload - supported by some init scripts, tries to reload the service configuration without a full restart

Running an init script without arguments should simply print its usage, along with supported commands.

At startup, /etc/init.d/rcS runs each of these scripts in order, with the start command. At shutdown, /etc/init.d/rcK runs each of these scripts in reverse order, with the stop command. See Boot Process for more details.

Automatic Functionality Detection

A thingOS system can include a relatively large number of init scripts. Some of them may however not be used due to the fact that their underlying functionality has not been configured. For example, S35wifi won't do anything unless there's a configured WiFi network in wpa_supplicant.conf.

Init scripts whose functionality is optional will check for a proper configuration at their beginning and will silently exit unless they find one. This allows shipping an OS with many optional services, allowing the user to configure them individually, as per their needs.

Manual Control

Init scripts can be manually run using commands such as /etc.init.d/<script> restart. There is however a more convenient way to control them, using the service helper command. For example, restarting the WiFi service can be done running:

service wifi restart

User Disabled Scripts

Given the purpose of thingOS, users probably shouldn't manually disable scripts on their systems. If however one wants a script to be disabled on a particular setup, the presence of a simple, empty file called /data/etc/no_<script> will prevent the automatic init script execution. For example, the presence of the following file will prevent the WiFi service from starting at boot:

/data/etc/no_S35wifi

Writing An Init Script

Being regular shell scripts, one can do anything in an init scripts. Following are a few pointers though on what to do and what not to do.

  • The best way to write a new init script is to take an existing one as example. S12udev is a simple init script and may be a good starting point

  • Define the configuration file(s) and executable program name(s) at the beginning and use them throughout the script.

  • The following line ensures the inclusion of the base for init scripts:

     test -n "${OS_VERSION}" || source /etc/init.d/base
    

    You'll then have functions like msg_begin, msg_done and msg_fail, which should be used for showing messages.

  • Use a case statement and always provide start, stop and usage commands.

  • Use the prepare_conf function to use a configuration file from the data partition, while first looking it up on boot and root partitions:

     prepare_conf ${CONF} ${SYS_CONF} ${BOOT_CONF}
    
  • Needles to say that init scripts must have a shebang and be executable.

Clone this wiki locally