萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> Tomcat監測腳本的實現示例

Tomcat監測腳本的實現示例

小編推薦的這篇文章介紹了Tomcat監測腳本的實現示例,非常實用,有興趣的同學快來看看吧

通過 Tomcat 監測腳本 按照一定的時間頻度來檢測Tomcat應用是否正常,如果進程不存在,則啟動 Tomcat;如果進程存在而訪問不正常,則殺掉進程,再啟動 Tomcat。

腳本的主體

 代碼如下復制代碼

vi/home/dev/ctl/tomcat-inspector/image.sh

#!/bin/bash

echo $(date'+%Y-%m-%d %H:%M:%S')

whoami

JAVA_HOME=/opt/java/jdk1.8.0_40

PATH=$JAVA_HOME/bin:$PATH

export PATH JAVA_HOME

port=8086

sn="tomcat-image-$port"

th="/home/dev/tomcat/$sn"

url="http://you-app-server/"

tomcat=`ps-ef | grep java | grep $sn | grep $port | wc-l`

if[ $tomcat-eq0]

then

  echo $sn stoped.

  echo starting $sn ......

  $th/bin/startup.sh

fi

if[ $tomcat-ge1]

then

  tpid=`ps-ef | grep java | grep $sn | grep $port | awk'{print $2}'`

  echo $snisrunning, pid=$tpid

  httpStatus=`curl-I $url2>/dev/null | grep HTTP | awk'{print $2}'`

  echo $httpStatus

  if[[-z"$httpStatus"|| $httpStatus-ge500]]

  then

    echo killing $sn ......

    kill-9$tpid

    echo starting $sn ......

    $th/bin/startup.sh

  fi

fi

設定執行頻度

 代碼如下復制代碼

crontab-e

*/5****/home/dev/ctl/tomcat-inspector/image.sh >>/home/dev/ctl/tomcat-inspector/logs/image.log2>&1

copyright © 萬盛學電腦網 all rights reserved