linux shell实现对tomcat进行重启或维护

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 -9 $TomcatID
      sleep 5
      $StartTomcat
    fi
  else
  echo "[error]process not exist!tomcat start automatically..."
  echo "[info]$StartTomcat,please wait a moment..."
  $StartTomcat
  fi
  echo "-------------------------------------------------------"
}

#call function Tomcat_Monitor
Tomcat_Monitor >> $TomcatMonitorLog

验证:

[root@logstash ~]# sh tomcat.sh 
[root@logstash ~]# ps -ef | grep tomcat
root       6203      1 60 17:06 pts/2    00:00:03 /usr/local/jdk-11.0.10/bin/java -Djava.util.logging.config.file=/opt/apache-tomcat-10.1.1/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -classpath /opt/apache-tomcat-10.1.1/bin/bootstrap.jar:/opt/apache-tomcat-10.1.1/bin/tomcat-juli.jar -Dcatalina.base=/opt/apache-tomcat-10.1.1 -Dcatalina.home=/opt/apache-tomcat-10.1.1 -Djava.io.tmpdir=/opt/apache-tomcat-10.1.1/temp org.apache.catalina.startup.Bootstrap start
root       6239   3551  0 17:06 pts/2    00:00:00 grep --color=auto tomcat
[root@logstash ~]# sh tomcat.sh 
[root@logstash ~]# cat /tmp/TomcatMonitor.log 
[info] begin tomcat...[2024-05-17 17:00:25]
[error]process not exist!tomcat start automatically...
[info]/opt/apache-tomcat-10.1.1/bin/startup.sh,please wait a moment...
Using CATALINA_BASE:   /opt/apache-tomcat-10.1.1
Using CATALINA_HOME:   /opt/apache-tomcat-10.1.1
Using CATALINA_TMPDIR: /opt/apache-tomcat-10.1.1/temp
Using JRE_HOME:        /usr/local/jdk-11.0.10
Using CLASSPATH:       /opt/apache-tomcat-10.1.1/bin/bootstrap.jar:/opt/apache-tomcat-10.1.1/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.
-------------------------------------------------------
[info] begin tomcat...[2024-05-17 17:06:25]
[error]process not exist!tomcat start automatically...
[info]/opt/apache-tomcat-10.1.1/bin/startup.sh,please wait a moment...
Using CATALINA_BASE:   /opt/apache-tomcat-10.1.1
Using CATALINA_HOME:   /opt/apache-tomcat-10.1.1
Using CATALINA_TMPDIR: /opt/apache-tomcat-10.1.1/temp
Using JRE_HOME:        /usr/local/jdk-11.0.10
Using CLASSPATH:       /opt/apache-tomcat-10.1.1/bin/bootstrap.jar:/opt/apache-tomcat-10.1.1/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.
-------------------------------------------------------
[info] begin tomcat...[2024-05-17 17:06:34]
[info]tomcat processid is:6203.
[error]visit error,return code is 200,error log is output to /dev/null
[error] restart tomcat
[info] begin tomcat...[2024-05-17 17:06:58]
[error]process not exist!tomcat start automatically...
[info]/opt/apache-tomcat-10.1.1/bin/startup.sh,please wait a moment...
Using CATALINA_BASE:   /opt/apache-tomcat-10.1.1
Using CATALINA_HOME:   /opt/apache-tomcat-10.1.1
Using CATALINA_TMPDIR: /opt/apache-tomcat-10.1.1/temp
Using JRE_HOME:        /usr/local/jdk-11.0.10
Using CLASSPATH:       /opt/apache-tomcat-10.1.1/bin/bootstrap.jar:/opt/apache-tomcat-10.1.1/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.
-------------------------------------------------------
[info] begin tomcat...[2024-05-17 17:08:03]
[info]tomcat processid is:6276.
[info] return code is 200,tomcat start successfully,the web is ok.
-------------------------------------------------------
[root@logstash ~]# 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黑要上天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值