Versions Compared

Key

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

...

pid를 얻고 실행 여부를 확인하고 시그널을 보내는 등의 일련의 작업을 간단한 쉘 스크립트로 구현했으니 shutdown.sh 대신 사용해도 된다.(gist 링크)

Code Block
titleshutdown_graceful.sh
linenumberstrue
collapsetrue
#!/bin/sh

killproc() {
    local servicename=$1
    local user=$2
    local signal="TERM"

    if [ "$#" = 0 ] ; then
        echo $"Usage: killproc {servicename} {user} {signal}"
        return 1
    fi

    if [ "$#" = 3 ]; then
        signal=$3
    fi
    PIDS=`ps -eaf|grep ${servicename}|grep -v grep|grep ${user}|awk '{print $2}'`
    ## process still running..
    for p in ${PIDS}
    do
        if [ ! -z ${p} ] && [ ${p} -gt 0 ];then
            echo "kill -${signal} ${p}"
            kill -${signal} ${p};
            return $?;
        else
             return 0;
        fi
    done
}

## tomcat instance name. recommended tomcat running with custom property (aka -Dcom.example.servicename=myWebApp )
SERVICE_NAME=myWebApp

## tomcat home
TC_HOME=/var/tomcat/tomcat-7.0.55
if [ ! -d ${TC_HOME} ];then
        TC_HOME=`pwd`
fi

## tomcat process owner name
USER=`whoami`

cd ${TC_HOME}
./bin/shutdown.sh >& /dev/null
sleep 1 # delay 1 sec for tomcat shutting down..

## 
for i in 1 2;do
    killproc ${SERVICE_NAME} ${USER}
    RET=$?
    if [ $RET = 0 ];then
        break;
    fi;
    sleep $i;
done

## if still running send KILL signal
killproc "${SERVICE_NAME}" "${USER}" "KILL"

...