Home>Article>Operation and Maintenance> How to use Crontab to regularly monitor and maintain Tomcat applications in Linux

How to use Crontab to regularly monitor and maintain Tomcat applications in Linux

王林
王林 forward
2023-05-14 08:07:05 1119browse

监测的应用接口: 新闻接口、天气接口
处理方法:应用接口不可用时自动重启tomcat,并发送告警邮件给相关人员

#!/bin/bash #--------------------------------------------------------- # 功能说明: # 监控指定http服务是否可用,如果不可用立即重启tomcat # # 使用说明: # 1. 将此脚本放置在/home/opentsp/crontab/目录下。 # 2. 修改脚本执行权下为可执行权限。 # 3. 添加到定时任务中,定时执行时间(建议为20分钟) # 4. 修改邮件发送人员信息列表(当服务重启时发邮件给相关人员) # - 周凌飞(2014-08-13) #--------------------------------------------------------- export lc_all=zh_cn.utf-8 #网站地址、参数 server_name="趣驾云接口服务" url_2="http://127.0.0.1/get_rss_news?p=%7b%27chid%27:%27tiyu%27%7d" keyworld_2='' url_3="http://127.0.0.1/get_json_weather?p=%7blon:116.407617,lat:39.993956,date:1%7d" keyworld_3='temperature' #邮件发送列表 mail_ary=( xxxxxxxxx@navinfo.com xxxxxxxxx@navinfo.com xxxxxxxxx@navinfo.com ) #接口调用失败的处理方法 function dofail(){ local ipinfo=$(ifconfig |sed -n '2p'|awk '{print substr($2,6)}'); # 发送邮件 for _v in ${mail_ary[*]} ; do echo "[$server_name 异常] - [$(date -d "0 min" +"%y-%m-%d %h:%m:%s")] - [请求地址: $1] - [请求返回码: $2]" | mail -s ${ipinfo}服务异常 ${_v} done # 写入日志 echo "[error] - [$(date -d "0 min" +"%y-%m-%d %h:%m:%s")] - 返回码[$2] - 重启tomcat服务" >> detect-http.log # 关闭tomcat sh /home/opentsp/crontab/ibr-shutdown.sh exit; } #请求超时时间设置 time_out=40 function docheck(){ local url_x=$1; local keyworld_x=$2; http_status_code=`curl -m $time_out -o /dev/null -s -w "%{http_code}" "${url_x}"` if [ $http_status_code != 200 ];then #请求失败 echo "-> fail - 返回码${http_status_code}"; dofail ${url_x} ${http_status_code}; else #服务器正常响应,检查返回内容 if curl -m ${time_out} -s ${url_x} | grep -q ${keyworld_x};then echo "-> success"; else echo "->> fail"; # 返回内容错误处理 dofail ${url_x} ${http_status_code}; fi fi } # #检查 - 新闻 docheck ${url_2} ${keyworld_2} #检查 - 天气 docheck ${url_3} ${keyworld_3}</pre>
      <p>将以上代码放入到linux的定时任务中即可,定时任务时间建议为20分钟一次。</p>
      <p>The above is the detailed content of How to use Crontab to regularly monitor and maintain Tomcat applications in Linux. For more information, please follow other related articles on the PHP Chinese website!</p>
     </div>
     <div class="nphpQianMsg">
      <div class="clear"></div>
     </div>
     <div class="nphpQianSheng">
      <span>Statement:</span>
      <div>
       This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete
      </div>
     </div>
    </div>
    <div class="nphpSytBox">
     <span>Previous article:<a class="dBlack" title="What are the most dangerous commands in Linux?" href="//m.sbmmt.com/m/faq/538756.html">What are the most dangerous commands in Linux?</a></span>
     <span>Next article:<a class="dBlack" title="What are the most dangerous commands in Linux?" href="//m.sbmmt.com/m/faq/538981.html">What are the most dangerous commands in Linux?</a></span>
    </div>
    <div class="nphpSytBox2">
     <div class="nphpZbktTitle">
      <h2>Related articles</h2>
      <em><a href="//m.sbmmt.com/m/article.html" class="bBlack"><i>See more</i><b></b></a></em>
      <div class="clear"></div>
     </div>
     <ul class="nphpXgwzList">
      <li><b></b><a href="//m.sbmmt.com/m/faq/363162.html" title="Teach you how to install Nginx server in Linux" class="aBlack">Teach you how to install Nginx server in Linux</a>
       <div class="clear"></div></li>
      <li><b></b><a href="//m.sbmmt.com/m/faq/363164.html" title="Detailed introduction to the wget command of Linux" class="aBlack">Detailed introduction to the wget command of Linux</a>
       <div class="clear"></div></li>
      <li><b></b><a href="//m.sbmmt.com/m/faq/363165.html" title="Detailed explanation of examples of using yum to install Nginx in Linux" class="aBlack">Detailed explanation of examples of using yum to install Nginx in Linux</a>
       <div class="clear"></div></li>
      <li><b></b><a href="//m.sbmmt.com/m/faq/363166.html" title="Detailed explanation of worker connections issues in Nginx" class="aBlack">Detailed explanation of worker connections issues in Nginx</a>
       <div class="clear"></div></li>
      <li><b></b><a href="//m.sbmmt.com/m/faq/363167.html" title="Detailed explanation of the process of installing python3 under linux" class="aBlack">Detailed explanation of the process of installing python3 under linux</a>
       <div class="clear"></div></li>
     </ul>
    </div>
   </div>
   <div class="nphpFoot">
    <div class="nphpFootBg">
     <ul class="nphpFootMenu">
      <li><a href="//m.sbmmt.com/m/"><b class="icon1"></b><p>Home</p></a></li>
      <li><a href="//m.sbmmt.com/m/course.html"><b class="icon2"></b><p>Course</p></a></li>
      <li><a href="//m.sbmmt.com/m/wenda.html"><b class="icon4"></b><p>Q&A</p></a></li>
      <li><a href="//m.sbmmt.com/m/login"><b class="icon5"></b><p>My</p></a></li>
      <div class="clear"></div>
     </ul>
    </div>
   </div>
   <div class="nphpYouBox" style="display: none;">
    <div class="nphpYouBg">
     <div class="nphpYouTitle">
      <span onclick="$('.nphpYouBox').hide()"></span>
      <a href="//m.sbmmt.com/m/"></a>
      <div class="clear"></div>
     </div>
     <ul class="nphpYouList">
      <li><a href="//m.sbmmt.com/m/"><b class="icon1"></b><span>Home</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/course.html"><b class="icon2"></b><span>Course</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/article.html"><b class="icon3"></b><span>Article</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/wenda.html"><b class="icon4"></b><span>Q&A</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/dic.html"><b class="icon6"></b><span>Dictionary</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/course/type/99.html"><b class="icon7"></b><span>Manual</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/xiazai/"><b class="icon8"></b><span>Download</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/faq/zt" title=""><b class="icon12"></b><span>Topic</span>
        <div class="clear"></div></a></li>
      <div class="clear"></div>
     </ul>
    </div>
   </div>
   <div class="nphpDing" style="display: none;">
    <div class="nphpDinglogo">
     <a href="//m.sbmmt.com/m/"></a>
    </div>
    <div class="nphpNavIn1">
     <div class="swiper-container nphpNavSwiper1">
      <div class="swiper-wrapper">
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/">Home</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/article.html" class="hover">Article</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/wenda.html">Q&A</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/course.html">Course</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/faq/zt">Topic</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/xiazai">Download</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/game">Game</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/dic.html">Dictionary</a>
       </div>
       <div class="clear"></div>
      </div>
     </div>
     <div class="langadivs">
      <a href="javascript:;" class="bg4 bglanguage"></a>
      <div class="langadiv">
       <a onclick="javascript:setlang('zh-cn');" class="language course-right-orders chooselan " href="javascript:;"><span>简体中文</span><span>(ZH-CN)</span></a>
       <a onclick="javascript:;" class="language course-right-orders chooselan chooselanguage" href="javascript:;"><span>English</span><span>(EN)</span></a>
       <a onclick="javascript:setlang('zh-tw');" class="language course-right-orders chooselan " href="javascript:;"><span>繁体中文</span><span>(ZH-TW)</span></a>
       <a onclick="javascript:setlang('ja');" class="language course-right-orders chooselan " href="javascript:;"><span>日本語</span><span>(JA)</span></a>
       <a onclick="javascript:setlang('ko');" class="language course-right-orders chooselan " href="javascript:;"><span>한국어</span><span>(KO)</span></a>
       <a onclick="javascript:setlang('ms');" class="language course-right-orders chooselan " href="javascript:;"><span>Melayu</span><span>(MS)</span></a>
       <a onclick="javascript:setlang('fr');" class="language course-right-orders chooselan " href="javascript:;"><span>Français</span><span>(FR)</span></a>
       <a onclick="javascript:setlang('de');" class="language course-right-orders chooselan " href="javascript:;"><span>Deutsch</span><span>(DE)</span></a>
      </div>
     </div>
    </div>
   </div>
   <!--顶部导航 end-->
  </div>
  <link rel="stylesheet" id="_main-css" href="//m.sbmmt.com/m/static/css/viewer.min.css" type="text/css" media="all">
 </body>
</html>