日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

shell实战之tomcat看门狗

發布時間:2023/12/2 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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監控日志
  • check_tom_run每隔30s檢測tomcat進程是否存在,log_info用于記錄tomcat運行日志和操作日志。
  • tomcat進程不存在時,執行start_tom去啟動。
  • 當日志文件大小大于指定大小時,則備份監控日志。
  • 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

    ?

    轉載于:https://www.cnblogs.com/webDepOfQWS/p/10655022.html

    總結

    以上是生活随笔為你收集整理的shell实战之tomcat看门狗的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。