Search code examples
javashellcentosjettycoredump

Bash shell script get core dump error


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.


Solution

  • 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. –