shell -- sample -- 关闭tomcat

#!/bin/bash
process_name="org.apache.catalina.startup.Bootstrap"
shutdown_call=0
function shutdownTomcat() {
    process_pid=`ps -ef | grep "$process_name" | grep -v "grep" | awk '{ print $2 }'`
    for i in $process_pid
    do
        echo "INFO: Kill the tomcat process [ $i ]"
        kill -9 $i
    done
    shutdown_call=1
}
function getProcessNum() {
    process_num=`ps -ef | grep "$process_name" | grep -v "grep" | wc -l`
    return $process_num
}
function checkYnAgain() {
    while true; do
    read -p "Please type \"Y\" or \"N\". " yn
    case $yn in
        [Yy]|[Yy][Ee][Ss] )
            shutdownTomcat
            break;;
        [Nn]|[Nn][Oo] )
            echo "exiting..."
            exit;;
    esac
    done
}
getProcessNum
tomcat_process_num=$?
if [ $tomcat_process_num -gt 0 ]; then
    echo "INFO: Tomcat server is running..."
    read -p "Do you want to shutdown tomcat server now [Y/N]? " yn
    case $yn in
        [Yy]|[Yy][Ee][Ss] )
            shutdownTomcat;;
        [Nn]|[Nn][Oo] )
            echo "exiting..."
            exit;;
        * )
            checkYnAgain;;
    esac
fi
getProcessNum
tomcat_process_num=$?
if [ $tomcat_process_num -gt 0 ]; then
    echo "ERROR: Fail to shutdown tomcat server."
    echo "You have to shutdown tomcat server or kill its process manually."
    exit 1
elif [ $shutdown_call -eq 1 ]; then
    echo "INFO: Shutdown tomcat server success."
fi

 

posted @ 2017-02-08 22:36  raindream  阅读(221)  评论(0编辑  收藏  举报