验证中...
Languages: Shell
Categories: Web开发技术
Latest update 2018-12-09 10:06
重启tomcat.sh
Raw Copy
#!/bin/sh
#脚本:重启tomcat,不同的tomcat修改tomcatPath即可
tomcatPath=/usr/local/tomcat8
echo "[info]重启tomcat的目录为:${tomcatPath}"
pid=`ps -ef | grep ${tomcatPath} | grep -v grep | awk '{print $2}'`
if [ $pid ]
then
{
#pid存在,开始关闭tomcat
echo "[info]tomcat进程号为:${pid}"
echo "[info]开始关闭tomcat"
kill -9 $pid
echo "[info]关闭tomcat成功"
sleep 2
echo "[info]开始重启tomcat"
$tomcatPath'/bin/startup.sh'
}
else
echo "[info]不存在pid,直接启动tomcat"
$tomcatPath'/bin/startup.sh'
fi
#重启tomcat结束
pid=`ps -ef | grep ${tomcatPath} | grep -v grep | awk '{print $2}'`
echo "重启tomcat结束,新的进程号pid:${pid}"
sleep 3
#看启动日志
tail -f $tomcatPath/logs/catalina.out
部署tomcat:自动备份发布war包
Raw Copy
#!/bin/bash
# func:tomcat自动备份发布war包
#注意
#修改tomcatPath和warDir的值,根据实际情况进行配置
# tomcat启动程序(这里注意tomcat实际安装的路径)
tomcatPath=/usr/local/tomcat8
# 项目名称
warDir=djd
# 获取tomcat进程ID
TomcatID=$(ps -ef | grep ${tomcatPath} | grep -v 'grep' | awk '{print $2}')
StartTomcat=${tomcatPath}/bin/startup.sh
Webapps=${tomcatPath}/webapps
WorkSpace=$(pwd)
warfile="$warDir.war"
#主函数
function monitor()
{
echo "[INFO]开始监控tomcat...[$(date +'%F %H:%M:%S')]"
echo '============部署tomcat的目录为:'${tomcatPath}'============'
if [ $TomcatID ]
then
echo "[INFO]当前tomcat进程ID为:$TomcatID"
kill -9 $TomcatID
echo "[INFO]杀死tomcat进程成功"
fi
sleep 3
backup
sleep 3
removeProject
sleep 3
upload
sleep 3
startTomcat
}
#启动tomcat
function startTomcat(){
echo "[INFO]准备启动tomcat"
if [ -f "$Webapps/$warfile" ]
then
$StartTomcat
tail -f ${tomcatPath}/logs/catalina.out
else
echo "[ERROR]tomcat启动失败,不存在war包:$Webapps/$warfile"
fi
}
#上传文件
function upload(){
echo "[INFO]开始上传文件"
cd $Webapps
if [ -f "$Webapps/$warfile" ]
then
echo "[INFO]已存在war包:$Webapps/$warfile,结束上传文件"
else
echo "[WARN]不存在war包:$Webapps/$warfile,准备上传文件"
rz
fi
cd $WorkSpace
}
#清除tomcat的项目缓存
function removeProject()
{
echo "[INFO]开始清理tomcat的项目缓存"
echo "[INFO]开始清理目录:$Webapps/$warDir"
if [ -d "$Webapps/$warDir" ]
then
rm -rf "$Webapps/$warDir"
else
echo "[ERROR]清理目录失败,不存在目录:$Webapps/$warDir"
fi
echo "[INFO]开始清理war包:$Webapps/$warfile"
if [ -f "$Webapps/$warfile" ]
then
rm -f "$Webapps/$warfile"
else
echo "[ERROR]清理war包失败,不存在war包:$Webapps/$warfile"
fi
echo "[INFO]清理tomcat的项目缓存完毕"
}
#备份原来的项目
function backup()
{
echo "[INFO]开始备份原来的项目...."
fileDate=`date +"%Y%m%d-%H%M%S"`
fileName=${warDir}"-backup-"${fileDate}
if [ -d "$Webapps/$warDir" ]
then
cd $Webapps
#压缩
tar czPvf ${fileName}.tar.gz ${Webapps}/${warDir}
cd $WorkSpace
echo "[INFO]备份完毕,备份的文件名:${fileName}"
else
echo "[ERROR]备份失败,不存在目录:$Webapps/$warDir"
fi
}
monitor
监控tomcat:定时5分钟一次监控Tomcat状态,自动重启
Raw Copy
#!/bin/sh
# function:自动监控tomcat进程,挂了就执行重启操作
# tomcat目录(这里注意tomcat实际安装的路径)
tomcatPath=/usr/local/tomcat8
# 定义要监控的页面地址
WebUrl=http://localhost:8080
# 定义日志输出
GetPageInfo=/dev/null
TomcatMonitorLog=/tmp/TomcatMonitor.log
# 获取tomcat进程ID
TomcatID=$(ps -ef | grep ${tomcatPath} | grep -v 'grep' | awk '{print $2}')
# tomcat_startup
StartTomcat=${tomcatPath}/bin/startup.sh
# tomcat缓存目录
TomcatCache=${tomcatPath}/work
Monitor()
{
echo "[info]开始监控tomcat...[$(date +'%F %H:%M:%S')]"
echo '============监控tomcat的目录为:'${tomcatPath}'============'
if [ $TomcatID ];then
echo "[info]tomcat进程ID为:$TomcatID."
# 获取返回状态码
TomcatServiceCode=$(curl -s -o $GetPageInfo -m 10 --connect-timeout 10 $WebUrl -w %{http_code})
if [ $TomcatServiceCode -eq 200 ];then
echo "[info]返回码为$TomcatServiceCode,tomcat启动成功,页面正常."
else
echo "[error]访问出错,状态码为$TomcatServiceCode,错误日志已输出到$GetPageInfo"
echo "[info]开始重启tomcat"
kill -9 $TomcatID # 杀掉原tomcat进程
sleep 3
#rm -rf $TomcatCache # 清理tomcat缓存
$StartTomcat
fi
else
echo "[warn]进程不存在!tomcat自动重启,请稍候..."
# 清理tomcat缓存
#rm -rf $TomcatCache
$StartTomcat
fi
echo "------------------------------"
}
Monitor>>$TomcatMonitorLog
#保存shell 名字为monitor.sh
#在终端输入 crontab -e
#在打开的脚本页输入(脚本执行时间和脚本位置):*/5 * * * * /shell/monitor.sh
#之后就会自动5分钟进行一次监控。
停止tomcat.sh
Raw Copy
#!/bin/sh
#脚本:停止tomcat,不同的tomcat修改tomcatPath即可
tomcatPath=/usr/local/tomcat8
echo "[info]停止tomcat的目录为:${tomcatPath}"
pid=`ps -ef | grep ${tomcatPath} | grep -v grep | awk '{print $2}'`
if [ "$pid" ]
then
{
#pid存在,开始关闭tomcat
echo "[info] tomcat进程号为:${pid}"
echo "开始关闭tomcat"
kill -9 $pid
echo "关闭tomcat成功"
}
else
echo "[info] tomcat未启动"
fi

Comment list( 0 )

You need to Sign in for post a comment

Help Search

183227_9af5e6a8_1826025 111910_4d91f001_1826025