Bash shell 脚本获取核心转储错误

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 [=12=]) && 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  in
   "m" | "ma" | "mas" | "mast" | "maste" | "master")
    #check configuration
    kill `ps -ef | grep "DMainProcess=true" | grep -v grep | awk '{print }'` >/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 }'` >/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:`
    JETTY_COUNT=`env | grep "^jetty[0-9]*="  | grep port: | wc -l`
    if [ $JETTY_COUNT -eq 0 ] ; then 
     echo jetty process with port  is not defined !
     return
    fi
    echo restart web $JETTY ;;
   jetty[0-9]*)
    #check configuration
    JETTY=`env | grep "^jetty[0-9]*=" | grep -v grep | grep `
    JETTY_COUNT=`env | grep "^jetty[0-9]*="  | grep  | wc -l`
    if [ $JETTY_COUNT -eq 0 ] ; then 
     echo jetty process name  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 }'` >/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=
   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 }'` >/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=
   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 [=13=]) && 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   

cd $OLDPWD  
exit

jetty.conf: #acturally 此文件用作 shell 脚本。

# 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"

我在我的应用程序上使用码头,同时 运行 几个进程,所以我写了重启 shell 脚本。

错误我在 basefuncs.sh 第 162 行随机得到核心转储,即:
for time in {1..60}
我检查了没有 core.PID 文件,并且
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?
无法防止核心转储错误。

感谢任何帮助。

终于找到核心转储的原因了。这是因为杀戮部分。 kill 不会立即终止进程,有一些时间延迟,这就是为什么我总是得到核心转储错误。我添加了一个for循环来检查这个进程是否被完全杀死,然后尝试启动该进程,成功解决了这个问题并且不再有核心转储。至于真正导致core dump的原因,我还是不太明白。任何信息将不胜感激。 –