Linux下tomcat守护程序,避免tomcat中断或内存溢出

这一两天现场实施的同事,发现系统的tomcat会因为内存溢出的情况出现假死的情况。现场的同事一时查不出问题,最后一招了写个脚本监控一下。

1 使用环境

操作系统:CentOS 4.8

JDK版本:j2sdk1.4.2

Tomcat版本:tomcat-5.0.28

2 监控脚本

#!/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="/home/test/tomcat-5.0.28";
check_page_url=http://127.0.0.1:8080/testweb/test.jsp;
pid_filt_pattern="tomcat";
guarder_dir="/home/test";
log_file="/home/test/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 -ef | grep ${pid_filt_pattern} | grep -v grep
	then
		kill -9 $(ps -ef | grep ${pid_filt_pattern} | grep -v grep | awk '{print $2}')
	fi
	
	# run start tomcat
	${tomcat_base_dir}/bin/startup.sh
	echo "$d success restart tomcat, the new pid is " >> ${log_file}
	ps -ef | grep ${pid_filt_pattern} | grep -v grep | awk '{print $2}' >> ${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

3 加入定时任务crontab

3.1 编辑任务

[test@cent4 ~]$ crontab -e

进入编辑crontab的vi界面,按i键进入插入模式,将如下代码复制到输入界面

*/20 * * * * "/home/test/deamon2tomcat.sh" > /dev/null 2>&1

注:每20分钟检查一次(可修改),加“> /dev/null 2>&1 ”是为了不让它发邮件。

3.2 保存退出

按Esc键进入命令模式,输入wq,回车后则会保存退出。

3.3 查看任务

最后查看任务,确认是否编辑成功。

[test@cent4 ~]$ crontab -l

*/20 * * * * "/home/test/deamon2tomcat.sh" > /dev/null 2>&1

4 jsp的内容

<%@ 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());
%>
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值