1. 程式人生 > >linux下tomcat部署、啟動、關閉的shell指令碼

linux下tomcat部署、啟動、關閉的shell指令碼

一、直接在tomcat的bin目錄下建立指令碼

1.部署war包指令碼deploy.sh:

 #!/bin/sh

war=$1
bin=$(cd `dirname $0`; pwd)

if [ ! -n "${war}" ]; then
   echo "***Usage: $0 [project.war]"
   exit 0
fi
if [ ! -f "${war}" ]; then
    echo "***Error: ${war} does not exist."
    exit 0
fi
if [ ! "${war##*.}" = "war" ]; then
    echo "***Error: ${war} is not a war file."
    exit 0
fi

echo "Deploy ${war##*/}..."
rm -rf ${bin}/../webapps/ROOT/ && unzip -qo ${war} -d ${bin}/../webapps/ROOT/
rm -rf ${bin}/../work/Catalina/localhost/
echo "Restart tomcat..."
exec ${bin}/restart.sh
 

2.重啟指令碼 restart.sh :

#!/bin/sh

bin=$(cd `dirname $0`; pwd)
pid=$(ps aux | grep tomcat | grep -v grep | grep -v restart | grep ${bin} | awk '{print $2}')
if [ -n "${pid}" ]; then
   echo "Shutdown..."
   sh ${bin}/shutdown.sh
   sleep 3

    pid=$(ps aux | grep tomcat | grep -v grep | grep -v restart | grep ${bin} | awk '{print $2}')
    if [ -n "${pid}" ]; then
        kill -9 ${pid}
        sleep 1
    fi
fi

echo "Startup..."
sh ${bin}/startup.sh
if [ "$1" = "-v" ]; then
    tail -f ${bin}/../logs/catalina.out
fi
 

二、部署指令碼單獨存放

例如:把部署指令碼放在 /opt/tomBin 目錄下

重啟指令碼的bin只需固定路徑即可,例如:bin="/usr/local/tomcat-8.5/bin"