Create Systemd Compatible SysV Init Script

There are scenarios where you want to create a backward compatible (sysV) init script. The location to store such script is located at /etc/init.d directory. This section guides you on how to create such script.

Comment Conventions

Every SysV init script now must have a comment convention for systemd compatibility purposes. The items are detailed in http://refspecs.linuxfoundation.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/initscrcomconv.html. Generally, the comment conventions should be:

### BEGIN INIT INFO
# Provides:       service-name
# Required-Start: $local_fs $network $remote_fs
# Required-Stop:  $local_fs $network $remote_fs
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Short-Description:  start and stop service-name
# Description:        service-name is a very fast and
#   reliable service used for demonstrating such usage.
### END INIT INFO

Conventional POSIX Shell Script

What comes after is the conventional POSIX Shell script. It reads the first argument to determine the action type. Then, you may execute your program accordingly. It is important to note that you must always ensure your program should not have any user-input prompt. Otherwise, the init will hang forever waiting for user input (which will never delivers).

Example:

case "$1" in
start)
        echo "Starting ${0##*/}"
        # run application you want to start
        /usr/local/bin/myprogram --start
        ;;
stop)
        echo "Stopping ${0##*/}"
        # run application you want to stop
        /usr/local/bin/myprogram --stop
        ;;
status)
        # run application you want to get status
        /usr/local/bin/myprogram --status
        ;;
reload)
        echo "Reloading ${0##*/}"
        # run application you want to reload
        /usr/local/bin/myprogram --reload
        ;;
force-reload)
        echo "Force-Reloading ${0##*/}"
        # run application you want to force reload
        /usr/local/bin/myprogram --force-reload
        ;;
restart)
        echo "Restarting ${0##*/}"
        # run application you want to restart
        /usr/local/bin/myprogram --stop
        /usr/local/bin/myprogram --start
        ;;
*)
        echo "Usage: $0 {start|stop|restart|status|reload|force-reload}"
        exit 1
        ;;
esac

exit 0

Putting All Together

If we put everything together, we should have the following script:

#!/bin/sh
### BEGIN INIT INFO
# Provides:       service-name
# Required-Start: $local_fs $network $remote_fs
# Required-Stop:  $local_fs $network $remote_fs
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Short-Description:  start and stop service-name
# Description:        service-name is a very fast and
#   reliable service used for demonstrating such usage.
### END INIT INFO

case "$1" in
start)
        echo "Starting ${0##*/}"
        # run application you want to start
        /usr/local/bin/myprogram --start
        ;;
stop)
        echo "Stopping ${0##*/}"
        # run application you want to stop
        /usr/local/bin/myprogram --stop
        ;;
status)
        # run application you want to get status
        /usr/local/bin/myprogram --status
        ;;
reload)
        echo "Reloading ${0##*/}"
        # run application you want to reload
        /usr/local/bin/myprogram --reload
        ;;
force-reload)
        echo "Force-Reloading ${0##*/}"
        # run application you want to force reload
        /usr/local/bin/myprogram --force-reload
        ;;
restart)
        echo "Restarting ${0##*/}"
        # run application you want to restart
        /usr/local/bin/myprogram --stop
        /usr/local/bin/myprogram --start
        ;;
*)
        echo "Usage: $0 {start|stop|restart|status|reload|force-reload}"
        exit 1
        ;;
esac

exit 0

Register Your Init Script

Once everything is ready, you can register your init script.

Set Executable Permission

You should set the script permission accordingly:

$ chmod 755 /etc/init.d/NameOfYourScript

Register Script

Once everything is done, you can register it:

$ update-rc.d NameOfYourScript defaults

OPTIONAL: Remove Script

In case you want to remove it:

$ update-rc.d -f  NameOfYourScript remove

That's all for creating a SysV and Systemd compatible init script.