Ubuntu init scripts (LSBInitScripts)

This section explains how to create a new init script for your debian based distro.
More information can be found here: https://wiki.debian.org/LSBInitScripts

Write the init script in /etc/init.d/. We will suppose we are creating a script called myService.

Thus the skeleton of script /etc/init.d/myService will be like this:

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


### BEGIN INIT INFO
# Provides:           myService
# Required-Start:     otherService anotherService
# Required-Stop:      otherService anotherService
# Should-Start:       maybeService
# Should-Stop:        maybeService
# Default-Start:      2 3 4 5
# Default-Stop:       0 1 6
# Short-Description:  Example of init service.
# Description:
#  Long description of my service. This service depends on otherService
# and anotherService to run. If it is installed it will also need that
# maybeService is running before myService.
### END INIT INFO


# Actions provided to make it LSB-compliant
case "$1" in
  start)
    echo "Starting myService "
    #Insert your startup routine here
    ;;
  stop)
    echo "Stopping script myService"
    #Insert your stop routine here
    ;;
  restart)
    echo "Restarting script myService"
    #Insert your restart routine here
    ;;
  force-reload)
    echo "Reloading script myService"
    #Insert your reload routine here
    ;;
  status)
    echo "Status of script myService"
    #Insert your stop routine here
    ;;
  *)
    echo "Usage: /etc/init.d/myService {start|stop|restart|force-reload|status}"
    exit 1
    ;;
esac

exit 0

The bock contained between:

### BEGIN INIT INFO
### END INIT INFO

specifies the different properties of this script, on which services depend and in which runlevels should be started or stopped.
Be sure to edit the entries corresponding to the dependent services: Required-Start, Required-Stop, Should-Start and Should-Stop. This is only an example.

Once you have completed the script you can execute:

$ update-rc.d myService defaults

This will create all the softlinks between /etc/rc?.d/ and your script. Those are the scripts which are executed at startup and shutdown.

If you want to avoid this script to execute at startup simply remove it from the rc.d directories:

$ update-rc.d -f myService remove

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s