-
Notifications
You must be signed in to change notification settings - Fork 48
Init Scripts
- Naming
- Internal Commands
- Automatic Functionality Detection
- Manual Control
- User Disabled Scripts
- Writing An Init Script
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.
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 astop
followed by astart
-
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.
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.
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
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
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
andmsg_fail
, which should be used for showing messages. -
Use a
case
statement and always providestart
,stop
and usage commands. -
Use the
prepare_conf
function to use a configuration file from thedata
partition, while first looking it up onboot
androot
partitions:prepare_conf ${CONF} ${SYS_CONF} ${BOOT_CONF}
-
Needles to say that init scripts must have a shebang and be executable.
- bluetooth.conf
- captive-portal.conf
- cpufreq.conf
- date.conf
- dnsmasq.conf
- docker-compose.yml
- dtoverlays
- dyndns-update.sh
- environment
- firewall.sh
- fstab.user
- hostapd.conf
- ifalias.conf
- localtime
- modprobe.conf
- modules
- mongodb.conf
- netwatch.conf
- ntp.conf
- os.conf
- proftpd.conf
- redis.conf
- smb.conf
- ssh/config
- ssh/sshd_config
- ssl/domain
- ssl/email
- static_ip.conf
- sysctl.conf
- toemmc.conf
- version
- watchdog.conf
- wpa_supplicant.conf