tomcat.sh
#!/bin/bash
#author:ztj
#date:2024-05-17
#description:function函数自动监控Tomcat进程,挂了就执行重启操作
TomcatID=$(ps -ef | grep tomcat | grep -w 'apache-tomcat-10.1.1' | grep -v 'grep' | awk '{print $2}')
#echo $TomcatID
#tomcat_startup
StartTomcat=/opt/apache-tomcat-10.1.1/bin/startup.sh
#define tomcat monitoring weburl
WebUrl=http://192.168.10.244:8080/
#define log output
GetPageInfo=/dev/null
TomcatMonitorLog=/tmp/TomcatMonitor.log
function Tomcat_Monitor() {
echo "[info] begin tomcat...[$(date +'%F %H:%M:%S')]"
if [ $TomcatID ];then
echo "[info]tomcat processid is:$TomcatID."
TomcatServiceCode=$(curl -s -o $GetPageInfo -m 10 --connect-timeout 10 $WebUrl -w %{http_code})
if [ $TomcatService -eq 200 ];then
echo "[info] return code is $TomcatServiceCode,tomcat start successfully,the web is ok."
else
echo "[error]visit error,return code is $TomcatServiceCode,error log is output to $GetPageInfo"
echo "[error] restart tomcat"
kill