CatchPlus / EPIC-API-v2

Version 2 of the EPIC PID Web Service
Other
4 stars 9 forks source link

Write a startup bash script #41

Open pieterb opened 11 years ago

pieterb commented 11 years ago

We could use a common startup script for running the webservice (calling rackup, deamonizing(?), storing a process ID etc.) This common startup script could then be invoked by the (more OS-specific) init-script.

cookie33 commented 10 years ago

We have some examples for this. For the handle service and the epic service. For the epic service it is: "/etc/init.d/epic"

!/bin/bash

#

epic server This shell script takes care of starting and stopping

epic server(s) (EPIC API).

#

chkconfig: - 58 74

description: epic server is the EPIC API daemon.

The handle system is used to store/retrieve/query handles which are

stored in a database. The EPIC API provides an easy interface.

BEGIN INIT INFO

Required-Start: mysqld hdl httpd

Required-Stop:

Short-Description: start and stop epic server

Description: epic server is the EPIC API daemon.

END INIT INFO

Source function library.

set -x

configSelect="*" prog=rackup epicBinDir=/opt/jruby/bin/

createDir() { for i in ${epicRunDir} ${epicLogDir}; do [ -d "$i" ] || { mkdir -p $i chown -R $epicUser:$epicUser $i } done }

status() { if [ -f $epicPID ] && ps -ef | grep -P "\b$(<$epicPID)\b" | grep -q java; then state="Running" statusValue=0 else state="NOT Running" statusValue=1 fi

    echo "Service  : $prog: for user: $epicUser with config: $epicConfig : $state"

    # return the status
    return $statusValue

}

start() { [ -x $epicBinDir/$prog ] || exit 5 [ -f /etc/sysconfig/epic-server ] && . /etc/sysconfig/epic-server statusValue=0

    # Start daemon.
    echo -n "Starting : $prog: for user: $epicUser with config: $epicConfig : "

    # check if server is already running
    status > /dev/null 2>&1 
    if [ $? -eq 0 ]
    then
            echo "Already started, NOT restarting"
            statusValue=1
    else
            su - $epicUser -c "cd $epicConfig ; rackup --pid=$epicPID >>$epicLogDir/rackup.log 2>&1  & disown %1"
            for i in {1..20} ; do
                    sleep 1
                    status > /dev/null 2>&1 
                    if [ $? -eq 0 ]
                    then
                            break
                    fi
            done

            sleep 1
            status > /dev/null 2>&1 
            if [ $? -eq 0 ]
            then
                    echo 'OK'
            else
                    echo 'FAILED'
                    statusValue=1
            fi 
    fi

}

stop() { statusValue=0

    # Stop daemon.
    echo -n "Stopping : $prog: for user: $epicUser with config: $epicConfig : "

    # check if server is already running
    status > /dev/null 2>&1 
    if [ $? -eq 1 ]
    then
            echo "Already stopped"
            statusValue=1
    else

            kill $(<$epicPID)
            sleep 2
            status > /dev/null 2>&1 
            if [ $? -eq 0 ]
            then
                    echo 'FAILED'
                    statusValue=1
            else
                    rm $epicPID > /dev/null 2>&1
                    echo 'OK'
            fi 
    fi

}

###########################################################################

main program

##########################################################################

check if parameter for config is set

if [ -n "$2" ] then configSelect="$2" fi

See how we were called.

case "$1" in start)

echo "Starting epic server(s)"

    for configuration in `find /etc/epic/${configSelect}.conf`; do
            . $configuration
            createDir
            start
    done
    ;;

stop)

echo "Stopping epic server(s)"

    for configuration in `find /etc/epic/${configSelect}.conf`; do
            . $configuration
            stop
    done
    ;;

status) for configuration in find /etc/epic/${configSelect}.conf; do . $configuration status $prog done ;; restart) for configuration in find /etc/epic/${configSelect}.conf; do . $configuration stop createDir start done ;; *) echo $"Usage: $0 {start|stop|status|restart} [config]" exit 2 esac

It works with a config file: "/etc/epic/handle1_epic_v2_test_prod.conf"

parameters

#

user to use for the epic service

epicUser=handle1 #

config

epicConfig=epic_v2_test_prod #

directory where the epic config (config.ru) resides

epicConfigDir=/home/${epicUser}/${epicConfig} #

directory where startup log of handle server resides

epicLogDir=/var/log/${epicUser}/${epicConfig} #

directory where pid file resides

epicRunDir=/var/run/${epicUser} #

pid file

epicPID=${epicRunDir}/${epicConfig}.pid

#

optional overrides. The default is set.

the epic server

prog=rackup

#

the location of the handle binary images

epicBinDir=/opt/jruby/bin