概述
今天分享一个tomcat管理脚本,主要是为了做tomcat启停、查看状态、web页面验证等功能,实现方式如下:
脚本内容:
#!/bin/bash
################################################################
# copyright by hwb --2019/8/12
# tomcat Bring up/down tomcat
#
# chkconfig: 2345 10 90
# description: start\stop\restart configured to start at boot time.
################################################################
# Source function library.
. /etc/init.d/functions
##防止乱码
export LANG=zh_CN.UTF-8
#指定tomcat
TOM=/home/tomcat8090
TOMCAT=$TOM/bin/startup.sh
PID=`ps -ef|grep $TOM|grep -v ’grep’|awk ’{print $2}’`
##定义要监控的页面地址
WebUrl=http://localhost:8090/epms/login
##日志输出
GetPageInfo=/tmp/web.info
if test -e $GetPageInfo; then
echo "[info] web页面检测日志输出 >>> $GetPageInfo"
else
touch $GetPageInfo
fi
################################################################
# 检测是否启动成功(成功的话页面会返回状态"200"),100秒无响应就不等待了
#curl [option] [url]
#-A/--user-agent <string> 设置用户代理发送给服务器
#-b/--cookie <name=string/file> cookie字符串或文件读取位置
#-c/--cookie-jar <file> 操作结束后把cookie写入到这个文件中
#-C/--continue-at <offset> 断点续转
#-D/--dump-header <file> 把header信息写入到该文件中
#-e/--referer 来源网址
#-f/--fail 连接失败时不显示http错误
#-o/--output 把输出写到该文件中
#-O/--remote-name 把输出写到该文件中,保留远程文件的文件名
#-r/--range <range> 检索来自HTTP/1.1或FTP服务器字节范围
#-s/--silent 静音模式。不输出任何东西
#-T/--upload-file <file> 上传文件
#-u/--user <user[:password]> 设置服务器的用户和密码
#-w/--write-out [format] 什么输出完成后
#-x/--proxy <host[:port]> 在给定的端口上使用HTTP代理
#-#/--progress-bar 进度条显示当前的传送状态
################################################################
TomcatServiceCode=`curl -s -o $GetPageInfo -m 100 --connect-timeout 100 $WebUrl -w %{http_code}`
clearcache()
{
rm -rf $TOM/work/Catalina/localhost
echo "[$(date +’%F %T’)] >>> Tomcat clean cache complete."
}
################################################################
#[注意]
#shell中利用-n来判定字符串非空,如果$PID不加""该if语句等效于if[-n]
#shell 会把它当成if[strl]来处理,-n自然不为空,判断为正,所以这里$PID一定要写成"$PID"
################################################################
start()
{
if [ -n "$PID" ]
then
echo "[$(date +’%F %T’)] >>> Tomcat is running."
else
clearcache
$TOMCAT
sleep 15
echo "[$(date +’%F %T’)] >>> Tomcat start complete."
fi
}
stop()
{
echo "[$(date +’%F %T’)] >>> Tomcat begin to stop."
if [ -n "$PID" ]
then
kill -9 $PID
echo "[$(date +’%F %T’)] >>> Tomcat stop complete"
else
echo "[$(date +’%F %T’)] >>> Tomcat is shutdown,not to stop"
fi
}
restart()
{
echo "[$(date +’%F %T’)] >>> Tomcat begin to restart."
stop
start
}
status()
{
if [ -n "$PID" ]
then
echo "[$(date +’%F %T’)] >>> Tomcat is running"
else
echo "[$(date +’%F %T’)] >>> Tomcat is down,need to be start"
fi
#检测页面
if [ $TomcatServiceCode -eq 200 ];then
echo "[info]页面返回码为$TomcatServiceCode,tomcat启动成功,测试页面正常"
else
echo "[error]tomcat页面出错,请注意...状态码为$TomcatServiceCode,错误日志已输出到$GetPageInfo"
fi
}
case "$1" in
"start")
start
;;
"stop")
stop
;;
"restart")
restart
;;
"status")
status
;;
*)
echo "请输入正确的参数start|stop|restart|status"
;;
esac
脚本效果
测试脚本如下:
./tomcat.sh start ./tomcat.sh stop ./tomcat.sh status ./tomcat.sh restart



后面会分享更多devops和DBA方面的内容,感兴趣的朋友可以关注下~
