#!/bin/bash
#The script is used to restart tomcat service.
#Date 2021-02-08
dir=/usr/local/tomcat/bin #定义tomcat目录
stop_log=/tmp/tomcat_stop.log #定义关闭日志
start_log=/tmp/tomcat_start.log #定义启动日志
#tomcat关闭函数
tomcat_stop() {
cd $dir
echo "tomcat stopping..."
./shutdown.sh &>$stop_log
i=0
#killall10次
while [ $i -lt 10 ]
do
if pgrep -l java &>/dev/null;then
killall java
sleep 1
echo -n "."
i=$[$i+1]
else
echo -e "\033[35m[ok]\033[0m"
break
fi
done
#kill -9 强制关闭可能会导致数据丢失,可以禁用。
if pgrep -l java &>/dev/null;then
pgrep java |xargs kill -9 2>/dev/null
sleep 1
pgrep java |xargs kill -9 2>/dev/null
fi
if pgrep -l java &>/dev/null;then
echo "The tomcat process can not be killed."
exit
fi
}
#tomcat启动函数
tomcat_start() {
cd $dir
echo -n "tomcat starting..."
#启动3次
./startup.sh &>$start_log
i=0
while [ $i -lt 3 ]
do
if ! pgrep java &>/dev/null;then
./startup.sh &>>$start_log
sleep 1
echo -n "."
i=$[$i+1]
else
echo -e "\033[35m[ok]\033[0m"
break
fi
done
if ! pgrep java &>/dev/null;then
cat $start_log
echo "The tomcat process failed to start,please check."
fi
}
read -p "Input yes to restart or no to exit: " answer
case "$answer" in
yes|YES)
echo "checking..."
#判断是否安装killall命令
if ! rpm -q psmisc &>/dev/null;then
echo "psmisc service installing..."
yum install -y psmisc
sleep 1
fi
tomcat_stop
tomcat_start
;;
no|NO)
echo "Bye."
exit
;;
*)
echo "please input yes or no."
;;
esac
原文:https://blog.51cto.com/11594671/2622774