0

basefuncs.sh:

#!/bin/bash
# this shell contains several base funcs that could be used by other shell
  # usage: add the following two line into ur shell(same folder with basefuncs.sh)
  # 
  #             BASE_SHELL=$(readlink -f $0) && BASE_SHELL=${BASE_SHELL%/wsmp_app/bin*}  && BASE_SHELL=$BASE_SHELL/wsmp_app/bin/basefuncs.sh && $BASE_SHELL
  #             source $BASE_SHELL
  #
  #then ,u can just invoke checkUser and other funcs as a command in ur own shell
  #
  echo
  USER=`whoami`
  jettyhome="/home/$USER/jetty"

  function checkUser()
  {
   if [ "$USER" = "root" ] ; then
    echo
    echo error ! current user is root !
    exit
   fi
  }

  function restartweb()
  {

   local JETTY
   local JETTY_COUNT

   case $1 in
   "m" | "ma" | "mas" | "mast" | "maste" | "master")
    #check configuration
    kill `ps -ef | grep "DMainProcess=true" | grep -v grep | awk '{print $2}'` >/dev/null 2>&1
    JETTY=`env | grep "^jetty[0-9]*=" | grep main`
    JETTY_COUNT=`env | grep "^jetty[0-9]*="  | grep main | wc -l`
    if [ $JETTY_COUNT -eq 0 ] ; then 
     echo main jetty process is not defined !
     return
    elif [ $JETTY_COUNT -gt 1 ] ; then
     echo muilt main jetty process is defined ! 
     env | grep "^jetty[0-9]*=" | grep -v grep | grep main
     return
    fi
    echo restart web master $JETTY
    ;;
   "s" | "sl" | "sla" | "slav" | "slave")
    #check configuration
    kill `ps -ef | grep "processName=$processName" | grep -v grep | grep -v "DMainProcess=true" | awk '{print $2}'` >/dev/null 2>&1
    JETTY=`env | grep "^jetty[0-9]*=" | grep -v main`
    JETTY_COUNT=`env | grep "^jetty[0-9]*="  | grep -v main | wc -l`
    if [ $JETTY_COUNT -eq 0 ] ; then 
     echo slave jetty process is not defined !
     return
    fi
    #reboot every jetty
    for process in $JETTY
    do
     echo restart web slave $process
     restartJetty $process 
    done
    #check is every process start up 
    for process in $JETTY
    do
     checkWebProcess $process 
    done
    return
    ;;
   [0-9]*)
    #check configuration
    JETTY=`env | grep "^jetty[0-9]*=" | grep -v grep | grep port:$1`
    JETTY_COUNT=`env | grep "^jetty[0-9]*="  | grep port:$1 | wc -l`
    if [ $JETTY_COUNT -eq 0 ] ; then 
     echo jetty process with port $1 is not defined !
     return
    fi
    echo restart web $JETTY ;;
   jetty[0-9]*)
    #check configuration
    JETTY=`env | grep "^jetty[0-9]*=" | grep -v grep | grep $1`
    JETTY_COUNT=`env | grep "^jetty[0-9]*="  | grep $1 | wc -l`
    if [ $JETTY_COUNT -eq 0 ] ; then 
     echo jetty process name $1 is not defined !
     return
    fi
    echo restart web $JETTY ;;
   "a"|"al"|"all")
    kill `ps -ef | grep "processName=jetty[0-9]*" | grep -v grep | awk '{print $2}'` >/dev/null 2>&1
    echo restarting all
    restartweb "master"
    restartweb "slave"
    return ;;
   *) 
    echo "usage : restart_web.sh [m,ma,mas,maste,master|s,sl,sla,slav,slave|jettyNumber|port|a,al,all]"
    return ;;
   esac

   restartJetty $JETTY 
   checkWebProcess $JETTY
  }

  #reboot a jetty process,arguments format: jetty0="main,port:8080,debug:9090,jmx:10010,log4j"
  function restartJetty(){
   local arg=$1
   local processName=${arg%=*}
   local processConf=${arg#*=}
   local isMainProcess="false"
   local command="java "
   #run in child process 
   #command="$command --exec "
   local OLD_IFS="$IFS"  && IFS="," && local args=($processConf) && IFS="$OLD_IFS"
   for arg in ${args[@]}
   do 
     local key=${arg%:*}
     local value=${arg#*:}
     case $key in 
     "port")
       local port=$value
       command="$command -Djetty.port=$value ";;
     "main")
       command="$command -DMainProcess=true"
       isMainProcess="true";;
     "debug")
       command="$command -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=$value -Xnoagent";;
     "jmx")
       command="$command -Djmxxxx ";;
     "log4j")
       command="$command -Dlog4j.debug ";;
     "*")
       echo unknow args $key:$value;;
     esac
   done
   command="$command -jar /home/$USER/jetty/start.jar -DprocessName=$processName -Xmx2048m -Djetty.home=$jettyhome --ini=/home/$USER/jetty/start.ini -Dlog4j.configuration=/home/$USER/wsmp_app/cfg/log4j.properties"
   kill `ps -ef | grep "processName=$processName" | grep -v grep | awk '{print $2}'` >/dev/null 2>&1
   local logDirectory="/home/$USER/wsmp_app/log/$processName-$port" 
   if [ $isMainProcess = "true" ] ; then 
    logDirectory="$logDirectory-Main"
   fi
   local logFile="$logDirectory/jetty-$(date "+%Y-%m-%d").log"
   #create log dir
   if [  ! -e $logDirectory ] ; then mkdir $logDirectory ; fi
   echo restarting `date "+%Y-%m-%d %H:%M:%S"` >> $logFile
   nohup $command  >> $logFile 2>&1 &
  }

  # check is jetty process start up ok
function checkWebProcess()
  { 
   local arg=$1
   local processName=${arg%=*}
   local processConf=${arg#*=}
   local OLD_IFS="$IFS"  && IFS="," && local args=($processConf) && IFS="$OLD_IFS"
   for arg in ${args[@]}
   do 
     local key=${arg%:*}
     local value=${arg#*:}
     if [ "$key" = "port" ] ; then 
      local port=$value
      break
     fi
   done
   echo -n checking process $processName on port $port
   #1 minites timeout
   for time in  {1..60}
   do
    app=`curl --connect-timeout 2 --noproxy 127.0.0.1 -s http://127.0.0.1:$port/wsmp/portallinkservlet | grep -i wsmp`
    echo -n "."
    if [ "$app" != "" ]; then
     echo ok
     return
    fi
    sleep 1
   done
   echo timeout, check logs in /home/$USER/wsmp_app/log/$processName-$port/
  }

restart_web.sh :

#!/bin/bash  
BASE_SHELL=$(readlink -f $0) && BASE_SHELL=${BASE_SHELL%/wsmp_app/bin*}  && BASE_SHELL=$BASE_SHELL/wsmp_app/bin/basefuncs.sh && $BASE_SHELL  
source $BASE_SHELL  

ENV_JETTYS=`env | grep jetty`  
for jetty in $ENV_JETTYS  
do  
    unset ${jetty%=*}   
done  

source ~/wsmp_app/cfg/jetty.conf  
checkUser  

OLDPWD=`pwd`  
cd ~/jetty/  
restartweb $1  

cd $OLDPWD  
exit

jetty.conf: #acturally this file is used as a shell script.

# supported args :    
#      
#      jettyX="main,port:8080,debug:9090,jmx:10010,log4j"    
#    
#        main : web mian process    
#       debug:remote debug,port xxx    
#        jmx  :jxm,port xxx,//TODO    
#        log4j:log4j debug   
#    
#    
#    
#   
#    
export jetty0="main,port:8080"    
export jetty1="port:8081"

I'm using jetty on my app,and running serveral processes at the same time , so I write restart shell script.

bug I randomly got core dump on basefuncs.sh line 162, which is:
for time in {1..60}
I checked there is no core.PID file,and
Segmentation fault (core dumped) error in unix shell script. Help finding bug?
How to generate a core dump in Linux when a process gets a segmentation fault?
could not prevent the core dump error.

anyhelp is appreciated.

Community
  • 1
  • 1
diego zhu
  • 51
  • 1
  • 7
  • This setup is actually supported with built-in as-shipped functionality in Jetty 9.x distribution btw. You no longer need this sort of shell script. – Joakim Erdfelt Jan 20 '15 at 15:55
  • I know that there is a setup.sh , but that is only for one jetty process, and I need multi process restart. – diego zhu Jan 21 '15 at 01:37
  • There's no `setup.sh` in jetty, just a `jetty.sh` that can be copied around and renamed (to make more service identifiers), along with a separation of `${jetty.home}` to `${jetty.base}` to allow these sorts of multi-service configurations. – Joakim Erdfelt Jan 21 '15 at 15:55
  • yes.you are right . but this is too hard-code style. What we are trying to do is just define how many process , and listening port of each process in just one file, not copy the whole directory and edit every jetty.sh , it's boring hand work. As you can see above, this way is clean and easy to modify when you need to check or modify how many process and which port of each process is listening. – diego zhu Mar 24 '15 at 09:33
  • and finally I find out the reason for core dump. It's because the kill part. kill will not kill the process right away , there is some time delay,that's why I always get core dump error. I add a for loop to check is the process is completely killed , and then try to start the process. which successfully fix this problem and no core dump any more. As for what really causes core dump , I still don't understand it. Any information will be appreciated. – diego zhu Mar 24 '15 at 09:37

1 Answers1

0

And finally I find out the reason for core dump. It's because the kill part. kill will not kill the process right away , there is some time delay,that's why I always get core dump error. I add a for loop to check whether this process is completely killed , and then try to start the process, which successfully fix this problem and no core dump any more. As for what really causes core dump , I still don't understand it. Any information will be appreciated. –

diego zhu
  • 51
  • 1
  • 7