Home>Article>Operation and Maintenance> How to use Crontab to regularly monitor and maintain Tomcat applications in Linux
监测的应用接口: 新闻接口、天气接口
处理方法:应用接口不可用时自动重启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}
将以上代码放入到linux的定时任务中即可,定时任务时间建议为20分钟一次。
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!