0

I have 3 services that need to be started (postgresql, redis, elasticsearch) before my app runs. My question is, can i consolidate the 3 into a file/script so everytime I want to run these services i'll just hit /etc/init.d/services start, and if im finished i'll just do a /etc/init.d/services stop and not stop the 3 of them manually?

3
  • yes sure, you will have to write your own script for that, a script based in /etc/init.d Nov 21, 2013 at 0:53
  • ouch! Is there any other way? I'm still a newbie at Ubuntu. I was expecting like a just a file that holds those services.
    – Finks
    Nov 21, 2013 at 0:55
  • it is not so complicated, in fact... Nov 21, 2013 at 1:07

2 Answers 2

1

yes sure, you will have to write your own script for that in /etc/init.d You will have to deal with something like this :

#!/bin/sh

# service name
SERVICE_NAME=Liferay
# directory where are stored service bin
SERVICE_DIRECTORY=/opt/Portal/bin
# startup service script
SERVICE_STARTUP_SCRIPT=startup.sh
# stop service script
SERVICE_SHUTDOWN_SCRIPT=shutdown.sh

usage()
{
        echo "-----------------------"
        echo "Usage: $0 (stop|start|restart)"
        echo "-----------------------"
}

if [ -z $1 ]; then
        usage
fi

service_start()
{
        echo "Starting service '${SERVICE_NAME}'..."
        OWD=`pwd`
        cd ${SERVICE_DIRECTORY} && ./${SERVICE_STARTUP_SCRIPT}
        cd $OWD
        echo "Service '${SERVICE_NAME}' started successfully"
}

service_stop()
{
        echo "Stopping service '${SERVICE_NAME}'..."
        OWD=`pwd`
        cd ${SERVICE_DIRECTORY} && ./${SERVICE_SHUTDOWN_SCRIPT}
        cd $OWD
        echo "Service '${SERVICE_NAME}' stopped"
}

case $1 in
        stop)
                service_stop
        ;;
        start)
                service_start
        ;;
        restart)
                service_stop
                service_start
        ;;
        *)
                usage
esac
exit 0

Just add your other services in start() stop() restart()

0

Even though it's valid, I don't think this is the proper way of doing this. initd was set so that you can set different priorities for the order of starting and stoping services.

If you look at chkconfig manual, you can see the priority for starting and stopping values meaning is. That will ensure that your services will be started in the order desired. The only consideration not made is, when a service fails to start, there's is no notification to the other services for this case. In that case, you can add code to poll for the status on your initd script to query the status of the services or simply just let your service complain later on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.