File jetty5.init of Package jetty5.openSUSE_12.1_Update

#!/bin/sh
#
# jetty5	This shell script takes care of starting and stopping jetty
### BEGIN INIT INFO
# Provides: jetty5
# Required-Start: $syslog $remote_fs $network $named
# Required-Stop: $syslog $remote_fs $network $named
# Default-Start: 3 5
# Default-Stop:  0 1 2 6
# Description: Webserver and Servlet Container
# Short-Description: start and stop jetty
### END INIT INFO
# chkconfig: - 80 20
# description: Webserver and Servlet Container
# processname: jetty
# pidfile: /var/run/jetty5/jetty5.pid
# config:  /etc/jetty5/jettyrc
#
#
# Portions from tomcat4.init by:
#
# Gomez Henri <hgomez@users.sourceforge.net>
# Keith Irwin <keith_irwin@non.hp.com>
# Nicolas Mailhot <nicolas.mailhot@one2team.com>
#
# Improved for SuSE (based on tomcat6 init script) by:
# Michal Vyskocil <mvyskocil@suse.cz>

# pulled from RHEL4 /etc/rc.d/init.d/functions
function checkpid() {
    local i
    for i in $* ; do
        if [ -d "/proc/${i}" ]; then
            return 0
        fi
    done
    return 1
}

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num><num>
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
. /etc/rc.status

# First reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running
# 
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.

# Get Jetty5's config
                                                                                                                                                             
export JETTYRC="/etc/jetty5/jettyrc"
                                                                                                                                                             
[ -r "$JETTYRC" ] && . "${JETTYRC}"

# Path to the jetty5 launch script (direct don't use the wrapper)
export JETTY_SCRIPT=/usr/bin/djetty5
JETTY_PROG=`basename $JETTY_SCRIPT`

# if JETTY_USER is not set, use jetty5
if [ -z "$JETTY_USER" ]; then
    JETTY_USER="jetty5"
fi
                                                                                                                                                             
# if JETTY_HOME is not set, use /usr/share/jetty5
if [ -z "$JETTY_HOME" ]; then
    JETTY_HOME="/usr/share/jetty5"
fi

if [ -z "$SHUTDOWN_WAIT" ]; then
    SHUTDOWN_WAIT=10
fi

if [ -z "$JETTY_PID" ]; then
    JETTY_PID=/var/run/jetty5.pid
fi

# For SELinux we need to use 'runuser' not 'su'
if [ -x "/sbin/runuser" ]; then
    SU="/sbin/runuser"
else
    SU="/bin/su"
fi


RETVAL=0

function parseOptions() {
    options=""
    options="$options $(
                 awk '!/^#/ && !/^$/ { ORS=" "; print "export ", $0, ";" }' \
                 $JETTYRC
             )"
    JETTY_SCRIPT="export QUIET='yes'; export JETTYRC=$JETTYRC; $options $JETTY_SCRIPT"
}

function start() {
    echo -n "Starting $JETTY_PROG ($JETTY_HOME): "

        if [ -f /var/run/rcjetty5 ] ; then
                if [ -f $JETTY_PID ]; then
                    read kpid < $JETTY_PID
                fi
                if [ -n "$kpid" ]; then
                        if checkpid $kpid 2>&1; then
                                echo "process already running"
                                rc_failed 0
                        else
                                echo "lock file found but no process running for pid $kpid, continuing"
                                rc_failed 7
                        fi
                fi
        fi

        # fix permissions on the log and pid files
        touch $JETTY_CONSOLE
        chown ${JETTY_USER}:${JETTY_USER} ${JETTY_CONSOLE}
        touch ${JETTY_PID}
        chown ${JETTY_USER}:${JETTY_USER} ${JETTY_PID}
        $SU -m - $JETTY_USER -c "$JETTY_SCRIPT start"

        RETVAL=$?
        if [ "$RETVAL" -eq 0 ]; then 
            rc_failed 0
            touch /var/run/rcjetty5
        else
            rc_failed 7
        fi
        rc_status -v
}


function stop() {
    echo -n "Stopping $JETTY_PROG ($JETTY_HOME): "
                                                                                                                                                             
    if [ -f /var/run/rcjetty5 ] ; then
      # fix permissions on the log and pid files
      touch $JETTY_CONSOLE
      chown ${JETTY_USER}:${JETTY_USER} ${JETTY_CONSOLE}
      touch ${JETTY_PID}
      chown ${JETTY_USER}:${JETTY_USER} ${JETTY_PID}
      $SU -m - $JETTY_USER -c "$JETTY_SCRIPT stop"
      RETVAL=$?

      if [ $RETVAL = 0 ]; then
        count=0;

        if [ -f $JETTY_PID ]; then
            read kpid < $JETTY_PID
        fi
        if [ -z "$kpid" ]; then
            kpid="$(ps U ${JETTY_USER} o pid,cmd | grep java | grep -v 'grep java' | cut -f 1 -d ' ')"
        fi
        if [ -n "$kpid" ]; then

            let kwait=$SHUTDOWN_WAIT

            until [ `ps --pid $kpid 2> /dev/null | grep -c $kpid 2> /dev/null` -eq '0' ] || [ $count -gt $kwait ]
            do
                echo "waiting for processes to exit";
                sleep 1
                let count=$count+1;
            done
                                                                                                                                                             
            if [ $count -gt $kwait ]; then
                echo "killing processes which didn't stop after $SHUTDOWN_WAIT seconds"
                kill -9 $kpid
            fi
            rc_failed 0
        else
            rc_failed 7
        fi
        rm -f /var/run/rcjetty5
        echo > $JETTY_PID
      else
          rc_failed 1
      fi
    fi
    rc_status -v
}

## Check status with checkproc(8), if process is running
## checkproc will return with exit status 0.

# Status has a slightly different for the status command:
# 0 - service running
# 1 - service dead, but /var/run/  pid  file exists
# 2 - service dead, but /var/lock/ lock file exists
# 3 - service not running

# NOTE: checkproc returns LSB compliant status values.
function status() {
    echo -n "Checking for $JETTY_PROG ($JETTY_HOME)"
    if [ -f $JETTY_PID ]; then
        read kpid < $JETTY_PID
    fi
    if [ -n "$kpid" ]; then
        if checkpid $kpid 2>&1; then
            rc_failed 0
        else
            rc_failed 2
        fi
    else
        #don't be dependent on procps
        #pid="$(/usr/bin/pgrep -u tomcat java)"
        pid="$(ps U ${JETTY_USER} o pid,cmd | grep java | grep -v 'grep java' | cut -f 1 -d ' ')"
        if [ -n "$pid" ]; then
            echo "$0 running (${pid}) but no PID file exists"
            rc_failed 0
        else
            rc_failed 3
        fi
    fi
    rc_status -v
}


# See how we were called.
case "$1" in
    start)
        parseOptions
        start
    ;;
    stop)
        parseOptions
        stop
    ;;
    try-restart)
        ## Stop the service and if this succeeds (i.e. the 
        ## service was running before), start it again.
        ## Note: try-restart is not (yet) part of LSB (as of 0.7.5)
        $0 status >/dev/null &&  $0 restart

        # Remember status and be quiet
        rc_status
    ;;
    restart)
        parseOptions
        stop
        sleep 2
        start
        rc_status
    ;;
    force-reload)
        ## Signal the daemon to reload its config. Most daemons
        ## do this on signal 1 (SIGHUP).
        ## If it does not support it, restart.

        echo -n "Reload service $JETTY_PROG $($JETTY_HOME)"
        ## if it supports it:
        #killproc -HUP $JETTY_PROG
        #touch /var/run/FOO.pid
        #rc_status -v

        ## Otherwise:
        $0 stop  &&  $0 start
        rc_status
    ;;
    reload)
        ## Like force-reload, but if daemon does not support
        ## signalling, do nothing (!)

        # If it supports signalling:
        #echo -n "Reload service FOO"
        #killproc -HUP $TOMCAT_BIN
        #touch /var/run/FOO.pid
        #rc_status -v

        ## Otherwise if it does not support reload:
        rc_failed 3
        rc_status -v
    ;;
    status)
        status
    ;;
    probe)
        ## Optional: Probe for the necessity of a reload,
        ## give out the argument which is required for a reload.
    ;;
    *)
        echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
        exit 1
    ;;
esac

rc_exit
#
#
# end
openSUSE Build Service is sponsored by