Endeca Hybris Init Script for Red Hat Systems

Feel free to use this script as you see fit.  It is by no means perfect, if you have some suggestions, I’d love to hear them.

Hybris does not include a Linux System V init script, so we had to write our own. Here’s the first ‘stable’ version of the script. This can be placed in /etc/init.d/hybris, most likely renamed to hybris_<env>. Ex. hybris_dev

It can be installed to run when the system reboots like this:

$ sudo chkconfig --add hybris

This will install the script by default at runlevels 3, 4 and 5.

#!/bin/bash
#
# rc file for starting hybris
#
# On most distributions, this file may be called:
# /etc/rc.d/init.d/hybris or /etc/init.d/hybris
#
# If you are running multiple Hybris instances
# You may want to give a more reasoneble name
# Such as  /etc/init.d/hybris_instance
#
# For Redhat-ish systems
#
# chkconfig: 345 99 99
# processname: java
# config: $HYBRIS_HOME/config/local.properties
# description: starts hybris server

# Set the environment level for hybris, this determines directory structure for systems with multiple installations.
# Example settings: {dev|qa|uat|prod}
ENV="dev"

# Make sure the server starts as a non-root user, set that here.
RUNAS="hybris"

# Set the base path to your hybris installation.
HYBRIS_HOME="/common/apps/hybris.${ENV}/hybris"

# Set the path to the Tomcat 6 startup script included with Hybris.
SERVSCRIPT="${HYBRIS_HOME}/bin/platform/hybrisserver.sh"

HYBRUNNING=`ps auxwww | grep "hybris.${ENV}" | grep "jmxremote" | wc -l`

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

# Make sure that this script is being run as the root user.
if (( `/usr/bin/id -u` != 0 )); then
{ echo "Sorry, this init script must be run as root.  Exiting...";
exit; }
fi


# Start the Hybris server.
start() {
        echo -n "Attempting to start up hybris_${ENV}: "
        echo
        sudo -u ${RUNAS} ${SERVSCRIPT} start
        touch /var/lock/subsys/hybris_${ENV}
        return 0
}

#Stop the Hybris server.
stop() {
        echo -n "Attempting to shut down hybris_${ENV}: "
        echo
        sudo -u ${RUNAS} ${SERVSCRIPT} stop
        rm -f /var/lock/subsys/hybris_${ENV}
        return 0
}

case "$1" in
    start)
        if [ ${HYBRUNNING} -eq 0 ]; then
                start
        else
                echo "Hybris appears to be running, and will not be started."
                echo "There are ${HYBRUNNING} related processes detected."
                echo "Try restarting the server, with '/etc/init.d/hybris restart'."
                exit 1
        fi
        ;;
    stop)
        if [ ${HYBRUNNING} -ne 0 ]; then
                stop
        else
                echo "Hybris does not appear to be running."
                echo "Total instances found running is ${HYBRUNNING}."
                exit 1
        fi
        ;;
    status)
        if [ ${HYBRUNNING} -ne 0 ]; then
                echo "Hybris appears to be running."
        else
                echo "Hybris is stopped."
        fi
        ;;
    restart)
        if [ ${HYBRUNNING} -ne 0 ]; then
                COUNTER=1
                while [ ${HYBRUNNING} -ne 0 ]; do
                        stop
                        echo "Sleeping for 10 seconds."
                        sleep 10
                        echo "Tried ${COUNTER} time(s)."
                        HYBRUNNING=`ps auxwww | grep "hybris.${ENV}" | grep "jmxremote" | wc -l`
                        COUNTER+=1
                done
                start
        else
                echo "Hybris appears to be stopped, please use '/etc/init.d/hybris start' to bring up the server."
                exit 1
        fi
        ;;
    reload)
        echo
        echo "To be implemented..."
        ;;
    *)
        echo "Usage: hybris {start|stop|status|reload|restart}"
        exit 1
        ;;
esac
exit $?