【问题标题】:Bash shell script get core dump errorBash shell 脚本获取核心转储错误
【发布时间】:2015-03-18 08:37:12
【问题描述】:

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: #实际上这个文件被用作一个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 脚本。

bug 我在 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?
无法防止核心转储错误。

感谢任何帮助。

【问题讨论】:

  • 顺便说一句,Jetty 9.x 发行版中的内置出厂功能实际上支持此设置。您不再需要这种 shell 脚本。
  • 我知道有一个 setup.sh ,但这只是一个码头进程,我需要多进程重启。
  • 码头中没有setup.sh,只有一个jetty.sh可以复制和重命名(以制作更多服务标识符),以及${jetty.home}${jetty.base}的分隔以允许这类多服务配置。
  • 是的。你是对的。但这太硬编码风格了。我们要做的只是在一个文件中定义多少进程,每个进程的监听端口,而不是复制整个目录并编辑每个 jetty.sh ,这是无聊的手工工作。如上所见,当需要检查或修改有多少进程以及每个进程的哪个端口正在监听时,这种方式干净且易于修改。
  • 最后我找到了核心转储的原因。这是因为杀戮部分。 kill 不会立即终止进程,有一些时间延迟,这就是为什么我总是得到核心转储错误。我添加了一个 for 循环来检查进程是否完全被杀死,然后尝试启动该进程。它成功地解决了这个问题并且不再有核心转储。至于真正导致 core dump 的原因,我还是不明白。任何信息将不胜感激。

标签: java shell centos jetty coredump


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2020-10-05
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 2013-05-17
    • 2014-08-04
    • 2012-11-19
    • 1970-01-01
    相关资源
    最近更新 更多