zoukankan      html  css  js  c++  java
  • shell实战之tomcat看门狗

    1、脚本简介

      tomcat看门狗,在tomcat进程异常退出时会自动拉起tomcat进程并记录tomcat运行的日志。

    1 函数说明:
    2         log_info:打印日志的函数,入参为需要在日志中打印的msg
    3         start_tom:启动tomcat的函数
    4         check_tom_run:每隔30s检测tomcat进程是否存在
    5         log_backup:备份tomcat监控日志
    1. check_tom_run每隔30s检测tomcat进程是否存在,log_info用于记录tomcat运行日志和操作日志。
    2. tomcat进程不存在时,执行start_tom去启动。
    3. 当日志文件大小大于指定大小时,则备份监控日志。

    2、前提条件

      a、需要一台Linux主机

      b、主机上已部署tomcat

      访问地址:http://localhost:8080/ 如果出现以下界面,则说明tomcat已成功启动。

      

    3、脚本代码

      脚本代码如下:

      1 #!/bin/bash
      2 #以下为多行注释
      3 : << !
      4 作用:tomcat watch dog,用于在tomcat进程退出后,自动拉起tomcat
      5 函数说明:
      6         log_info:打印日志的函数,入参为需要在日志中打印的msg
      7         start_tom:启动tomcat的函数
      8         check_tom_run:每隔10s检测tomcat进程是否存在
      9         log_backup:备份tomcat监控日志
     10 !
     11 
     12 curr_path=`pwd`
     13 #tomcat的安装路径
     14 tom_path=/home/stephen/InstallPath/apache-tomcat-8.5.39
     15 
     16 
     17 #定义打印日志的函数
     18 function log_info(){
     19 local curr_time=`date "+%Y-%m-%d %H:%M:%S"`
     20 log_file=${curr_path}/tom_running.log
     21 #判断日志文件是否存在
     22 if [ -e ${log_file} ]
     23    then
     24    #检测文件是否可写
     25    if [ -w ${log_file} ]
     26    then
     27        #若文件无写权限则使用chmod命令赋予权限
     28        chmod 770 ${log_file}
     29    fi
     30 else
     31    #若日志文件不存在则创建
     32    touch ${log_file}
     33 fi
     34 #写日志
     35 local info=$1
     36 echo "${curr_time}  `whoami` [Info] ${info}">>${log_file}
     37 }
     38 
     39 function start_tom(){
     40         log_info "Begin to start tomcat."
     41         cd ${tom_path}/bin
     42         log_info "cd ${tom_path}/bin"
     43         sh  startup.sh
     44         log_info "sh  startup.sh"
     45         #使用ps命令+awk获取tomcat的PID
     46         tom_pid=`ps -aux|grep apache-tomcat|grep -v grep|awk '{print $2}'`
     47         if  [ -z ${tom_pid} ]
     48             then
     49             sh startup.sh
     50         fi
     51         log_info "End to start tomcat."
     52 }
     53 
     54 #如果tomcat_pid为零,则说明tomcat进程不存在,需要去重启
     55 function check_tom_run()
     56 {
     57 tom_pid=`ps -aux|grep apache-tomcat|grep -v grep|awk '{print $2}'`
     58 echo ${tom_pid}
     59 if [ -z ${tom_pid} ]
     60 then
     61    echo "tomcat process is not running."
     62    #调用函数start_tom
     63    start_tom
     64    log_info "tomcat process is not running."
     65    #打印日志
     66 else
     67    echo "tomcat process is running"
     68    #打印日志
     69    log_info  "tomcat process is running"
     70 fi
     71 }
     72 #备份日志
     73 function log_backup(){
     74 cd ${curr_path}
     75 log_name=tom_running.log
     76 #获取当前日志的大小
     77 log_size=`ls -all|grep -v ${log_name}.|grep  ${log_name}|awk '{print $5}'`
     78 echo ${log_size}
     79 #当日志大于150MB时进行备份并清空旧的日志
     80 expect_size=`expr 150 * 1024 * 1024`
     81 echo ${expect_size}
     82 if [ ${log_size} -gt ${expect_size} ]
     83    then
     84    log_info "Begin to backup log."
     85    local ct=`date "+%Y-%m-%d-%H-%M-%S"`
     86    cp ${log_name} ${log_name}.${ct}
     87    log_info "cp ${log_name} ${log_name}.${ct}"
     88    #使用gzip命令压缩日志
     89    gzip -q  ${log_name}.${ct}  ${log_name}.${ct}.gz
     90    #清空旧日志
     91    cat  /dev/null > ${log_name}
     92    log_info "cat  /dev/null > ${log_name}"
     93 fi
     94 }
     95 
     96 #隔30s循环执行check_tom_1run
     97 while [ 1 ]
     98 do
     99   check_tom_run
    100   log_backup
    101   #休眠30s
    102   sleep 30
    103 done
    View Code

    4、运行结果

      4.1、运行脚本命令如下

    1 chmod +x  /tomcatWatchDog.sh
    #&表示脚本后台运行
    2 ./tomcatWatchDog.sh &

      4.2、新打开一个窗口,杀掉tomcat进程

    #获取tomcat的PID
    ps -ef|grep  tomcat
    #kill进程,PID为tomcat进程ID
    kill -9 PID

      4.3、查看tom_running文件,从日志来看tomcat进程已自动拉起。

     1 2019-04-04 15:23:19  stephen [Info] tomcat process is running
     2 2019-04-04 15:23:20  stephen [Info] tomcat process is running
     3 2019-04-04 15:23:34  stephen [Info] tomcat process is running
     4 2019-04-04 15:24:04  stephen [Info] tomcat process is running
     5 2019-04-04 15:24:34  stephen [Info] Begin to start tomcat.
     6 2019-04-04 15:24:34  stephen [Info] cd /home/stephen/InstallPath/apache-tomcat-8.5.39/bin
     7 2019-04-04 15:24:34  stephen [Info] sh  startup.sh
     8 2019-04-04 15:24:34  stephen [Info] End to start tomcat.
     9 2019-04-04 15:24:34  stephen [Info] tomcat process is not running.
    10 2019-04-04 15:25:04  stephen [Info] tomcat process is running
    11 2019-04-04 15:25:34  stephen [Info] tomcat process is running

      4.4、当日志文件大小大于指定大小时,会备份日志文件。

    1 -rwxr-xr-x  1 stephen stephen   2679 4月   4 14:57 tomcatWatchDog.sh*
    2 -rwxrwx---  1 stephen stephen 573893 4月   4 15:28 tom_running.log*
    3 -rwxr-x---  1 stephen stephen   7597 4月   4 12:43 tom_running.log.2019-04-04-12-43-53.g
  • 相关阅读:
    每日一篇文献:Robotic pick-and-place of novel objects in clutter with multi-affordance grasping and cross-domain image matching
    每日一篇文献:Intuitive Bare-Hand Teleoperation of a Robotic Manipulator Using Virtual Reality and Leap Motion
    每日一篇文献:Virtual Kinesthetic Teaching for Bimanual Telemanipulation
    HEBI Robotic Arm VR Teleoperation
    「iQuotient Case」AR device teleoperated robotic arm
    VR and Digital Twin Based Teleoperation of Robotic Arm
    HEBI Robotic Arm VR Teleoperation
    Human Robot Interaction
    Immersive Teleoperation Project
    机器人演示学习
  • 原文地址:https://www.cnblogs.com/webDepOfQWS/p/10655022.html
Copyright © 2011-2022 走看看