Subversion Repositories DevTools

Rev

Blame | Last modification | View Log | RSS feed

#!/bin/sh
### BEGIN INIT INFO
# Provides:          JatsBuildDaemon
# Required-Start:    $all
# Required-Stop:
# Default-Start:     3 4 5
# Default-Stop:      0 1 6
# X-Interactive:     true
# Short-Description: Jats Build Deamon
# Description:       The Jats Build Daemon provides automated build services
### END INIT INFO

#set -x
#
# Start, stop buildtool
#
# Note: This must be a 'sh' compatible script
#       Solaris8 uses sh to source the script. It is not run at startup
#
# Note: This file should be installed into /etc/init.d
#
# Note: Use /etc/init.d/buildtool setup
#        to perform additional setup of adding files to setup sequence
#
#set -x
BUILDUSR=buildadm
WRAPPERSCRIPT=/work/buildtool/abtwrapper


pid=0
#pid null if not running, pid if running
#   Get the process group of the wrapper script
check_running() {
        pid=`pgrep abtwrapper`
        [ -n "$pid" ] && pid=`ps -o pgid= -p $pid`
}

#
# Start daemons.
#
start() {
    check_running
    if [ "$pid" ]; then
          echo 'Running'
    else
          echo 'Starting'
          if [ "$USER" != "$BUILDUSR" ]; then SU="su - ${BUILDUSR} -c "; else SU="/bin/sh -c"; fi
          $SU "${WRAPPERSCRIPT}" &
    fi
}

#
# Stop daemons.
#   Terminate the process group of the controlling script
#
stop() {
    check_running
    echo "check_running returned $pid"

    if [ "$pid" ]; then
        for kpid in $pid ; do
            echo "Killing $kpid"
            kill -9 -$kpid 2>/dev/null 1>&2
        done
    else
        echo 'Not running'
    fi
}

#
#   Status the daemons
#
status() {
    check_running

    if [ "$pid" ]; then
          echo 'buildtool running'
    else
          echo 'buildtool not running'
    fi
}

#
#   Setup init levels
#   Linux and Solaris are different
#
setup() {
    if [ ! -f /etc/init.d/buildtool ] ; then
        echo "Error: /etc/init.d/buildtool does not exist"
        exit 1
    fi

    mtype=`uname`
    if [ -x /usr/sbin/update-rc.d ] ; then mtype=Ubuntu; fi
    
    case "$mtype" in
    Ubuntu)
        /usr/sbin/update-rc.d -f buildtool remove
        /usr/sbin/update-rc.d buildtool defaults
        ;;
    
    Linux)
        for ii in 2 3 4 5; do
            file=/etc/rc.d/rc$ii.d/S80buildtool
            [ -f $file ] || ln -s /etc/init.d/buildtool $file
        done
        
        for ii in 0 1 6 ; do 
            file=/etc/rc.d/rc$ii.d/K30buildtool
            [ -f $file ] || ln -s /etc/init.d/buildtool $file
        done
        ;;
        
    SunOS)
        for ii in 3; do
            file=/etc/rc$ii.d/S80buildtool
            [ -f $file ] || ln -s /etc/init.d/buildtool $file
        done
        
        for ii in 0 1 S ; do 
            file=/etc/rc$ii.d/K30buildtool
            [ -f $file ] || ln -s /etc/init.d/buildtool $file
        done
        ;;
        
    *)
        echo "$0 setup - Unknown machine type: $mtype"
        exit 1
        ;;
    esac        
}

#
#   Determine user command
#
case "$1" in
  start)    start
            ;;
  stop)     stop
            ;;
  status)   status
            RETVAL=$?
            ;;  
  restart)  stop
            start
            ;;
  setup)    setup
            ;;   
  *)        echo "Usage: $0 {start|stop|restart|status|setup}"
            exit 1
            ;;
esac

exit $RETVAL