【转】Linux下程序监控tomcat状态,必要时自动重启tomcat
google了一番,再编写测试N番,得出以下结果。
一、环境
操作系统:Linux(redhat 8)
JDK版本:j2sdk
Tomcat版本:tomcat-
二、监控脚本
#!/bin/bash
#
# Keep watch at tomcat's status,
# automatically restart it if it dead or out of memory.
export PATH=$PATH:/sbin:/bin:/usr/sbin:/usr/bin;
tomcat_port=":8080 ";
tomcat_base_dir="/opt/tomcat-";
check_page_url=http://192.168.2.11:8080/test.jsp;
pid_filt_pattern="tomcat";
guarder_dir="/opt/tomcat_guarder";
log_file="/opt/tomcat_guarder/tomcat_run_log.log";
d=$(date +%F" "%T);
# init the log file
touch ${log_file}
do_restart_tomcat() {
# first, try to shutdown it anyway!
${tomcat_base_dir}/bin/shutdown.sh
# second, check if the tomcat pid still exist, if yes then kill it!
if ps -elf | grep ${pid_filt_pattern} | grep -v grep
then
kill -9 $(ps -elf | grep ${pid_filt_pattern} | grep -v grep | awk '{print $4}')
fi
# run start tomcat
${tomcat_base_dir}/bin/startup.sh
echo "$d success restart tomcat, the new pid is " >> ${log_file}
ps -elf | grep ${pid_filt_pattern} | grep -v grep | awk '{print $4}' >> ${log_file}
echo >> ${log_file}
}
# first, check if the tomcat si listen on the port
if netstat -ln | grep ${tomcat_port}
then
# init the check result file
if [ -e ${guarder_dir}/checkResult.tmp ]
then
cat /dev/null > ${guarder_dir}/checkResult.tmp
else
touch ${guarder_dir}/checkResult.tmp
fi
# try to get the check result
wget -b -o wget.log -O ${guarder_dir}/checkResult.tmp ${check_page_url}
# wait 5 second to let the get check result job done.
sleep 5
# check the result
workflag=$(cat ${guarder_dir}/checkResult.tmp |grep ServerStillWorking)
memoryflag=$(cat ${guarder_dir}/checkResult.tmp |grep LessOfMemory)
if [ "$workflag" == "" ]; then
echo "$d can not found [ServerStillWorking] in the check result, try to restart tomcat ......" >> ${log_file}
do_restart_tomcat
elif [ "$memoryflag" == "" ]; then
echo "$d can not found [LessOfMemory] in the check result, the tomcat server may out of memory, try to restart it ......" >> ${log_file}
do_restart_tomcat
fi
else
echo "$d found the tomcat not listen on ${tomcat_port}, try to restart it ......" >> ${log_file}
do_restart_tomcat
fi
三、加入定时任务,每20分钟检查一次
*/20 * * * * "/opt/tomcat_guarder/tomcat_guarder.sh" > /dev/null 2>&1
注:加“> /dev/null 2>&1 ”是为了不让它发邮件。
四、test.jsp的内容
<%--
Created by IntelliJ IDEA.
User: Haydo Liu
Date:
Time: 12:20:21
Desc: 测试服务器是否正常的页面
--%>
<%@ page contentType="text/html;charset=GBK" %>
<%@ page import="java.util.*" %>
<%
out.println("<br>");
out.println("<center><h1>");
out.println("The Web Server is Running!<br><br>");
out.println("</h1></center>");
out.println("<br><br>");
out.println("ServerStillWorking");//标记字符!
long maxMemory = Runtime.getRuntime().maxMemory()/1024/1024; //java虚拟机能取得的最大内存
long totalMemory = Runtime.getRuntime().totalMemory()/1024/1024;//java虚拟机当前取得的内存大小
long freeMemory = Runtime.getRuntime().freeMemory()/1024/1024; //java虚拟机所占用的内存中的空闲部分
long usedMemory = totalMemory-freeMemory; //java虚拟机当前实际使用的内存大小
out.println("<br><br>Max Momery is: "+ maxMemory +"M");
out.println("<br>Total Memory is: "+ totalMemory +"M");
out.println("<br>Used Memory is: "+ usedMemory +"M");
out.println("<br>Free Memory is: "+ freeMemory +"M");
out.println("<br><br>");
if(usedMemory < maxMemory)
{
out.println("LessOfMemory");//标记字符!
}
else
{
out.println("OutOfMemory");//标记字符!
}
out.println("<br><br>");
out.println(new java.util.Date());
out.println("<br>");
out.println(TimeZone.getDefault().getDisplayName());
%>
五、参考
http://www.xgdown.com/article/53/138443_1.htm
http://www.chinalinuxpub.com/bbs/showthread.php?t=46321
http://feixingqi.blog.51cto.com/661368/133545
http://www.linuxtone.org/html/07/t-1707.html
http://security.ctocio.com.cn/tips/420/8257920_1.shtml
http://linux.die.net/man/1/wget
http://community.eapps.com/showthread.php?p=300
http://community.eapps.com/showthread.php?t=126
http://www.heritage-tech.net/762/automatically-restart-dead-services-via-bash-scripting/