测试环境的tomcat重启和清除temp,work目录以及logs目录下的日志是功能测试人员的一个很烦人的日常工作,即机械又耗时(重复次数多了之后)。所以写了该脚本用于tomcat的重启以及缓存、日志目录的清空,最后一步根据实际情况选择是否打印catalina.out日志以及是否需要修改时间为制定时间。只需要配置tomcat_name变量为需要执行操作的tomcat目录即可。
该脚本也曾被用于我们的jenkins持续集成平台中部署程序完成后的tomcat重启
脚本见下:
#!/bin/sh
#
#2015-10-22
#andre.yang
#This script is used to commemorate my forever boss Pufan
tomcat_name=/home/tomcat_eomobi_S2S
tomcat_dir=`dirname $0`
if [ -d $tomcat_name'/temp/' ]
then
tempPath= $tomcat_name'/temp/'
else
tempPath=''
fi
workPath=$tomcat_name'/work/'
logPath=$tomcat_name'/logs/catalina.out'
delelogPath=$tomcat_name'/logs/'
pid0=`ps -ef|grep "java"|grep $tomcat_name|awk '{print $2}'`
#if tomcat is not run ,start it
if [ "$pid0" = "" ]
then
{
echo "tomcat is not run"
if [ -d "$tempPath" ]
then
rm -rf "$tempPath"
echo "**********************dele temp ******************"
fi
if [ -d "$tempPath" ]
then
echo "**********************dele temp faild******************"
else
echo "**********************dele temp success******************"
fi
if [ -d "$workPath" ]
then
echo "**********************dele work******************"
rm -rf "$workPath"
fi
if [ -d "$workPath" ]
then
echo "**********************dele work faild******************"
else
echo "**********************dele work success****************"
fi
echo "**********************start tomcat******************"
echo -n "chance date : "
read date
sudo date -s $date
cd $tomcat_name
./bin/startup.sh
sleep 2
#print log
echo -n "is or not print log:y/n: "
read return
if [ $return = y ]
then
tail -f $logPath -n -500
else
echo "**********************tomcat restart success********************"
fi
}
else
{
#print pid and shutdown tomcat
echo pid is $pid0
ps -ef|grep $tomcat_name|grep -v grep|cut -c 9-15|xargs kill -9
#check tomcat's status and return result
pid1=`ps -ef|grep "java"|grep $tomcat_name|awk '{print $2}'`
if [ "$pid1" = "" ]
then
echo "kill sucess!"
if [ -d "$tempPath" ]
then
rm -rf "$tempPath"
echo "**********************dele temp ******************"
fi
if [ -d "$tempPath" ]
then
echo "**********************dele temp faild******************"
else
echo "**********************dele temp success******************"
fi
if [ -d "$workPath" ]
then
rm -rf "$workPath"
echo "**********************dele work ******************"
fi
if [ -d "$workPath" ]
then
echo "**********************dele work faild******************"
else
echo "**********************dele work success****************"
fi
cd $tomcat_dir
#check temp's dir and delte it
echo -n "chanle date : "
read date
sudo date -s $date
cd $tomcat_name
./bin/startup.sh
else
{
echo pid is $pid1
echo "kill faild";
}
fi
sleep 2
#print log
echo -n "is or not print log:y/n: "
read return
if [ $return = y ]
then
tail -f $logPath -n -500
else
echo "**********************tomcat restart success********************"
fi
}
fi