Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Note, test the script as below, and make sure all the commands are in the right place, e.g. /bin/kill, /bin/ps, etc

Code Block
#!/bin/bash

export JAVA_HOME=/opt/appserv/java6
export PATH=$JAVA_HOME/bin:$PATH

pidLocation=/opt/grouperLoader/grouperLoader.pid
gshBinDirectory=/opt/appserv/tomcat_3b/webapps/grouperWs/WEB-INF/bin
loaderLog=/opt/grouperLoader/grouperLoader.log

cd $gshBinDirectory
theDate=`date`
/usr/bin/nohup $gshBinDirectory/gsh -loader >> $loaderLog 2>&1 &
pid=$!
echo $pid > $pidLocation
echo >> $loaderLog
echo "##############################" >> $loaderLog
echo "## $theDate: started Grouper loader... ($pid)" >> $loaderLog
echo "##############################" >> $loaderLog
echo >> $loaderLog

...

Code Block
#!/bin/sh
#
# Startup script for the Tomcat Server
#
# chkconfig: - 86 14
# description: Grouper loader service
# processname:
# pidfile:
# config:
# Tomcat
# description: Keeps Grouper permissions in sync with file on linux server

#################### CUSTOMIZE THIS FOR YOUR ENV #######################
# the user this script should run as

shouldRunAs=appadmin

pidLocation=/opt/grouperLoader/grouperLoader.pid

#note, this should not contain the gsh command, just the parent directory
gshBinDirectory=/opt/appserv/tomcat_3b/webapps/grouperWs/WEB-INF/bin

#only run on daemon box of cluster.  note, customize this for your env
source /home/appadmin/bin/requireDaemon.sh

loaderLog=/opt/grouperLoader/grouperLoader.log

grouperLoaderBootstrapScript=/opt/appserv/grouperLoader/grouperLoader.sh
# grouperLoaderBootstrapScript is a way to su as the real user to run the loader
# it has these contents
#
# #!/bin/bash
#
# export JAVA_HOME=/opt/appserv/java6
# export PATH=$JAVA_HOME/bin:$PATH
#
# pidLocation=/opt/grouperLoader/grouperLoader.pid
# gshBinDirectory=/opt/appserv/tomcat_3b/webapps/grouperWs/WEB-INF/bin
# loaderLog=/opt/grouperLoader/grouperLoader.log
#
# cd $gshBinDirectory
# theDate=`date`
# /usr/bin/nohup $gshBinDirectory/gsh -loader >> $loaderLog 2>&1 &
# pid=$!
# echo $pid > $pidLocation
# echo >> $loaderLog
# echo "##############################" >> $loaderLog
# echo "## $theDate: started Grouper loader... ($pid)" >> $loaderLog
# echo "##############################" >> $loaderLog
# echo >> $loaderLog





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

# function to kill a pid, wait for exit...
killPid(){
  pidToKill=$1
  /bin/kill $pidToKill

  #loop until the program is dead
  waitingForExit=true
  iterator=1

  while [ "$waitingForExit" == "true" ]; do

    # xargs will trim the string
    processIdToKill=`/bin/ps -fp $pidToKill | grep gsh | grep loader | grep -v /bin/ps | cut -c10-15 | xargs`
    if [ -n "$processIdToKill" ]; then
      echo Waiting for exit...
    else
      # lets just make sure: (sometimes process isnt found with -fp...)
      /bin/kill -KILL $pidToKill > /dev/null 2>&1

      waitingForExit=false
    fi

    if [ "$iterator" -gt "10" ]; then
      waitingForExit=false
    fi

    let "iterator += 1"
    if [ "$waitingForExit" == "true" ]; then
      sleep 1
    fi
  done

  processIdToKill=`/bin/ps -fp $pidToKill | grep gsh | grep loader | grep -v /bin/ps | cut -c10-15 | xargs`
  if [ -n "$processIdToKill" ]; then
    /bin/kill -KILL $pidToKill
    sleep 1

    processIdToKill=`/bin/ps -fp $pidToKill | grep gsh | grep loader | grep -v /bin/ps | cut -c10-15 | xargs`
    if [ -n "$processIdToKill" ]; then
      echo "Cannot kill process: $processIdToKill"
      exit 1
    fi  

  fi

}

# get the process id of the gsh process, put into the variable theGshProcessId
# if pass "true" as arg, then exit if there are multiple running
gshProcessId(){

  if [ "$1" == "true" ]; then
    numberOfProcesses=`/bin/ps -ef | grep $gshBinDirectory | grep gsh | grep loader | grep -v /bin/ps | grep edu.internet2.middleware.grouper.app.gsh.GrouperShellWrapper | wc -l`
    if [  "$numberOfProcesses" -gt "1" ]; then
      echo "Grouper loader is running multiple times!!!"
      exit 1
    fi
  fi


  theGshProcessId=`/bin/ps -ef | grep $gshBinDirectory | grep gsh | grep loader | grep -v /bin/ps | grep edu.internet2.middleware.grouper.app.gsh.GrouperShellWrapper | head -1 | cut -c10-15 | xargs`
}

# get the parent process id of the gsh process, put into the variable theGshParentProcessId
gshParentProcessId(){
  theGshParentProcessId=`/bin/ps -ef | grep /bin/sh | grep $gshBinDirectory | grep gsh | grep loader | grep -v /bin/ps | head -1 | cut -c10-15 | xargs`
}



runningUser=`/usr/bin/whoami`

 
# we only want appadmin or root to be able to do this
if [ "$runningUser" != "$shouldRunAs" ]; then
 
  if [ "$runningUser" != "root"  ]; then
 
    echo "ERROR: only user $shouldRunAs or root can run administer the grouperLoader service: '$runningUser'"
    echo
    exit 1
    
  fi
 
fi
 
case "$1" in
  start)
 
    gshProcessId "true"
    gshParentProcessId

    # if this is true, then it is running...
    if [ -n "$theGshProcessId" ]; then

      # lets see if the process id matches
      if [ -f "$pidLocation" ]; then
        pidInFile=`cat $pidLocation`
      
        if [ -n "$theGshParentProcessId" ]; then
          if [ "$theGshParentProcessId" -eq "$pidInFile" ]; then
            echo "Grouper loader is running already, parent process id: $theGshParentProcessId, process id: $theGshProcessId"
            exit 1
          fi
        fi

      fi
      echo "Grouper loader is already running, but pid doesnt match file.  Parent process id: $theGshParentProcessId, process id: $theGshProcessId, parent process id in file: $pidInFile"
      exit 1
    fi

    echo "Starting Grouper loader service..."

    # if not appadmin, then we must be root
    if [ ! "$runningUser" == "$shouldRunAs" ]; then
      su $shouldRunAs -c $grouperLoaderBootstrapScript
    else
      # if not root then we must be appadmin
      $grouperLoaderBootstrapScript
    fi
    echo "Grouper loader is running..."
    ;;
  stop)


    waitingForExitStop=true
    iteratorStop=1
    wasRunning=false
    while [ "$waitingForExitStop" == "true" ]; do

      gshProcessId
      if [ -n "$theGshProcessId" ]; then
        wasRunning=true
        echo "Grouper loader is running with processId: $theGshProcessId, waiting for exit..."
        theDate=`date`
        if [ -f $loaderLog ]; then
          echo >> $loaderLog
          echo "##############################" >> $loaderLog
          echo "## $theDate: stopping Grouper loader... ($theGshProcessId)" >> $loaderLog
          echo "##############################" >> $loaderLog
          echo >> $loaderLog
        fi

        killPid $theGshProcessId
      else
        waitingForExitStop=false
      fi

      if [ "$iteratorStop" -gt "10" ]; then
       
        echo "Cannot stop Grouper loader!"
        exit 1
      fi

      let "iteratorStop += 1"
      if [ "$waitingForExitStop" == "true" ]; then
        sleep 1
      fi
    done

    waitingForExitStop=true
    iteratorStop=1

    while [ "$waitingForExitStop" == "true" ]; do

      gshParentProcessId
      if [ -n "$theGshParentProcessId" ]; then
        echo "Grouper loader parent is running with processId: $theGshProcessId, waiting for exit..."
        killPid $theGshParentProcessId
      else
        waitingForExitStop=false
      fi

      if [ "$iteratorStop" -gt "10" ]; then
       
        echo "Cannot stop Grouper loader parent!"
        exit 1
      fi

      let "iteratorStop += 1"
      if [ "$waitingForExitStop" == "true" ]; then
        sleep 1
      fi
    done
    if [ -f $pidLocation ]; then
      rm $pidLocation
    fi
    if [ "true" == "$wasRunning" ]; then
      echo "Grouper loader is stopped"
    else
      echo "Grouper loader was not running"
    fi
    ;;
  status)

    gshProcessId "true"
    gshParentProcessId

    # if this is true, then it is running...
    if [ -n "$theGshProcessId" ]; then

      # lets see if the process id matches
      if [ -f "$pidLocation" ]; then
        pidInFile=`cat $pidLocation`
      
        if [ -n "$theGshParentProcessId" ]; then
          if [ "$theGshParentProcessId" == "$pidInFile" ]; then
            echo "Grouper loader is running, parent process id: $theGshParentProcessId, process id: $theGshProcessId"
            exit 0
          fi
        fi

      fi
      echo "Grouper loader is running, but pid doesnt match file.  Parent process id: $theGshParentProcessId, process id: $theGshProcessId, parent process id in file: $pidInFile"
      exit 0
    fi
    # lets see if the process id matches
    if [ -f "$pidLocation" ]; then
      pidInFile=`cat $pidLocation`
    
      if [ -n "$theGshParentProcessId" ]; then
        echo "Grouper loader is not running, but there is a process id: $pidInFile in the pid file: $pidLocation"
        exit 1
      fi
    fi
    echo "Grouper loader is not running"
    exit 0

    ;;
  restart)
    echo -n "Restarting Grouper loader: "
    $0 stop
    sleep 5
    $0 start
    echo "done."
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1
esac

...