2.8. Starting an application at boot

To run scripts or start services during system boot, Vesta Gateways implement the standard Linux init subsystem, which in turn is based on the traditional UNIX System V init mechanism. If you wish to have an additional application or service start automatically when the Gateway boots, you may add a new initialization script for the application or service in question in the Gateway’s /etc/init.d directory. You can then create Links to this script in the appropriate rc.d directories using the Gateway’s update-rc.d utility, ensuring that the newly-added script will be called during system initialization.

2.8.1. A sample init script

The following is an example of a simple init script that could be used to start the application sampleApp, whose binary is located in /usr/loca/bin, as a background service at system start-up. The script also specifies the procedure to be used to stop sampleApp at system shutdown:

#! /bin/sh
# /etc/init.d/start_sample_app
#

# Carry out specific functions when asked to by the system
case "$1" in
start)
   echo "Starting sampleApp "
   exec /usr/local/bin/sampleApp -s &
      ;;
stop)
   echo "Stopping sampleApp"
   start-stop-daemon --stop --name sampleApp --quiet
   ;;
(*))
   echo "Usage: /etc/init.d/start_sample_app {start|stop}"
   exit 1
   ;;
esac
exit 0

Completed init scripts should be placed in the Gateway’s /etc/init.d directory, with execute (e.g. chmod 755) permission.

2.8.1.1. More complex init scripts

Of course, Vesta init scripts can be far more sophisticated than the sample given above. Vesta Gateways ship with a number of init scripts already present in the /etc/init.d directory, and these serve as useful examples when creating new init scripts for the Gateway.

2.8.3. Adding init scripts to your Yocto build

If you build a customized version of Rigado’s Vesta developer image by obtaining and modifying Rigado’s Yocto build (see the Build Your Own Image section of this tutorial), it’s worth noting that Yocto allows a version of the update-rc.d command to be used in the do_install() section of bitbake recipes. This allows developers to specify that rc.d links to a designated init script should be included in the root file system generated during a Yocto build.

For example, the following bitbake recipe excerpt instructs Yocto to install the init scripts setmac.sh and hciattach.sh in /etc/init.d, and assign start numbers 60 and 61 to these scripts in the rc2.d-rc5.d directories.

do_install() {
   install -d ${D}${sysconfdir}/init.d

   install -m 0755 setmac.sh ${D}${sysconfdir}/init.d
   update-rc.d -r ${D} setmac.sh start 60 2 3 4 5 .

   install -m 0755 hciattach.sh ${D}${sysconfdir}/init.d
   update-rc.d -r ${D} hciattach.sh start 61 2 3 4 5 .
}