Create Unix Service Under init.d

Case Scenario: I have a customized service, “sphinx” installed. I had to manually install it from scripts. Now i wish to create service for sphinx program to operate as regular Linux service.
Following below process I have done it.

Create a script and place in /etc/init.d (/etc/init.d/sphinx). The script should have the following:

#!/bin/bash
# chkconfig: 2345 20 80
# description: Sphinx Searchd Service

# Source function library.
. /etc/init.d/functions

start() {
        exec /usr/local/sphinx/bin/searchd
}

stop() {
        exec /usr/local/sphinx/bin/searchd --stop
}

case "$1" in
    start)
       start
       ;;
    stop)
       stop
       ;;
    restart)
       stop
       start
       ;;
    status)
        exec /usr/local/sphinx/bin/searchd --status
       ;;
    *)
       echo "Usage: $0 {start|stop|status|restart}"
esac

exit 0

The structure of the scripts is pretty easy to understand.

  • The command which will start the service. It should be placed inside start() block.
     /usr/local/sphinx/bin/searchd 
  • The command which will stop the service. It should be placed inside stop() block.
     /usr/local/sphinx/bin/searchd --stop 
  • The command which will show the status of the service. It should be placed at the right position inside switch statements.
     /usr/local/sphinx/bin/searchd --status