Realize the function of monitoring whether tomcat is down/stopped and restarted under Linux

Realize the function of monitoring whether tomcat is down/stopped and restarted under Linux



Preface

First of all, we need to know what preparations are needed to implement the function


One, install crontab

The crontab on Linux is a timed task tool that can help you implement the function of executing a certain script in a timed loop

1. Confirm whether crontab is installed

Enter the command line crontab -l.
If it reports "command not found", it means that the crontab service has not been installed. If it reports "no crontab for root", it means that the crontab service has been installed, and there is no need to install it again.

2. Install crontab

Enter the following commands in turn
yum install vixie-cron
yum install crontabs

3. Use crontab

Talk about several commonly used commands
service crontab start start crontab service
service crontab stop stop crontab service
service crontab reload restart crontab service
service crontab status view the status of crontab service
crontab -e Edit the timing task, here is a template, you can make some modifications:
*/1 * * * * /tmp/xxx/xxx.sh (This represents execution once every minute, the following is the path of the script, the cron expression can be generated online by Baidu)

4. Add crontab to self-start at boot

Use the command ntsysv, move the keyboard up and down to select, press the space to select or cancel, press Tab to switch the cursor to the list and confirm the cancelNote that if there is a self-start of the tomcat configuration, it will be automatically cancelled after saving, so after saving, you need to set the tomcat self-start again, and use the command chkconfig --add script name to complete the tomcat self-start configuration

Two, edit the monitor.sh script

1.monitor.sh script template

The code is as follows (example):

#!/bin/sh
#tomcat监控脚本,记录tomcat运行状况,并在系统出现异常的时候重启服务
# 获取线程ID,执行该命令,可以得到tomcat线程,grep -w用于执行单词搜索
TomcatID=$(ps -ef | grep java |grep tomcat |grep -w 'DataExchange'|grep -v 'grep'|awk '{print $2}')
. /etc/profile #此行必不可少,若少了,tomcat无法自动重启
# tomcat启动的脚本文件,一般位于tomcat服务器bin目录下面
StartTomcat=/usr/tomcat/xxx/bin/startup.sh

# 项目的一个方法,该方法只要能够请求到,则返回成功
WebUrl=http://localhost:8080/xxx

#tomcatwork空间
TomcatCache=/usr/tomcat/xxx/work

# 日志输出地址
GetPageInfo=/dev/null
TomcatMonitorLog=/tmp/TomcatMonitor.log

#监控的代码
Monitor()
{
  #输出到日志文件,并将时间保存
  echo "[info]开始监控tomcat...[$(date +'%F %H:%M:%S')]"
  #查看tomcatID,如果存在,则进入处理逻辑,如果不存在,则重启tomcat
  if [ -n "$TomcatID" ];then
    #记录日志
    echo "[info]tomcat进程ID为:$TomcatID."
    # 请求项目,$WebUrl是相应的请求地址,该命令执行之后会得到相应的线程ID
    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 "[error]开始重启tomcat"
        #杀掉相应的进程
        #kill -9 $TomcatID  # 杀掉原tomcat进程
      for id in $TomcatID
        do
        kill -9 $id # 杀掉原tomcat进程
        echo "killed $id"
      done
        sleep 1
	#清理work空间
        rm -rf $TomcatCache # 清理tomcat缓存
	#重启
        $StartTomcat
        echo "[info] tomcat开始重启."
    fi
  else
    echo "[error]进程不存在!tomcat自动重启...$TomcatID"
    echo "[info]$StartTomcat,请稍候......"
    #rm -rf $TomcatCache
    $StartTomcat
  fi
  echo "------------------------------"
}
Monitor>>$TomcatMonitorLog

The script is that I have integrated and modified some scattered scripts on the Internet. If there is anything wrong, please point it out. Learn together and make progress together~

2. Use crontab to complete timing tasks

Here you can execute the script first to see if it works, then use the above crontab -e command to add the script, then execute service crontab reload, and then check the log output to see if the function is completed


Guess you like

Origin blog.csdn.net/qq_44941808/article/details/114062026