deluan
9/29/2010 - 4:10 PM

Init script for Hudson CI Server

Init script for Hudson CI Server


#!/bin/bash
#
# Startup script for Hudson
#
# chkconfig: - 84 16
# description: Hudson CI server

# Source function library.
#. /etc/rc.d/init.d/functions
. /lib/lsb/init-functions

[ -z "$JAVA_HOME" -a -x /etc/profile.d/java.sh ] && . /etc/profile.d/java.sh

HUDSON_HOME=/opt/hudson
HUDSON_PORT=8081
HUDSON_PREFIX=/hudson
HUDSON_OPTS=-Dfile.encoding=UTF-8

WAR="$HUDSON_HOME/hudson.war"
LOG="/var/log/hudson.log"
LOCK="/var/lock/hudson.lck"
export HUDSON_HOME

RETVAL=0

pid_of_hudson() {
        ps auxwww | awk '/java/ && /hudson/ && !/grep/ {print $2}'
}

start() {
    [ -e "$LOG" ] && cnt=`wc -l "$LOG" | awk '{ print $1 }'` || cnt=1

    echo -n $"Starting hudson: "

    cd "$HUDSON_HOME"
    nohup java $HUDSON_OPTS -server -jar "$WAR" --httpPort=$HUDSON_PORT --prefix=$HUDSON_PREFIX >> "$LOG" 2>&1 &

    while { pid_of_hudson > /dev/null ; } &&
          ! { tail -n +$cnt "$LOG" | grep -q 'Winstone Servlet Engine .* running' ; } ; do
        echo -n .
        sleep 1
    done

    pid_of_hudson > /dev/null
    RETVAL=$?
    if [ $RETVAL = 0 ]; then
        echo "  success"
        touch "$LOCK"
    else
        echo "  failure!"
    fi
    echo
}

stop() {
    echo -n "Stopping hudson: "

    pid=`pid_of_hudson`
    [ -n "$pid" ] && kill $pid
    RETVAL=$?
    cnt=10
    while [ $RETVAL = 0 -a $cnt -gt 0 ] &&
          { pid_of_hudson > /dev/null ; } ; do
        sleep 1
        ((cnt--))
        echo -n .
    done

    if [ $RETVAL = 0 ]; then
        echo "  success"
        rm -f "$LOCK"
    else
        echo "  failure!"
    fi
    echo
}

status() {
    pid=`pid_of_hudson`
    if [ -n "$pid" ]; then
        echo "hudson (pid $pid) is running..."
        return 0
    fi
    if [ -f "$LOCK" ]; then
        echo $"${base} dead but subsys locked"
        return 2
    fi
    echo "hudson is stopped"
    return 3
}

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status
    ;;
  restart)
    stop
    start
    ;;
  *)
    echo $"Usage: $0 {start|stop|restart|status}"
    exit 1
esac

exit $RETVAL