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

http://blog.csdn.net/huang_xw/article/details/7086308


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

1 使用环境

操作系统:CentOS 4.8

JDK版本:j2sdk1.4.2

Tomcat版本:tomcat-5.0.28

2 监控脚本

[plain]  view plain copy print ?
  1. #!/bin/bash  
  2. #  
  3. # Keep watch at tomcat's status,   
  4. # automatically restart it if it dead or out of memory.  
  5.   
  6. export PATH=$PATH:/sbin:/bin:/usr/sbin:/usr/bin;  
  7. tomcat_port=":8080 ";  
  8. tomcat_base_dir="/home/test/tomcat-5.0.28";  
  9. check_page_url=http://127.0.0.1:8080/testweb/test.jsp;  
  10. pid_filt_pattern="tomcat";  
  11. guarder_dir="/home/test";  
  12. log_file="/home/test/tomcat_run_log.log";  
  13. d=$(date +%F" "%T);  
  14.   
  15. # init the log file  
  16. touch ${log_file}  
  17.   
  18. do_restart_tomcat() {  
  19.     # first, try to shutdown it anyway!  
  20.     ${tomcat_base_dir}/bin/shutdown.sh  
  21.       
  22.     # second, check if the tomcat pid still exist, if yes then kill it!  
  23.     if ps -ef | grep ${pid_filt_pattern} | grep -v grep  
  24.     then  
  25.         kill -9 $(ps -ef | grep ${pid_filt_pattern} | grep -v grep | awk '{print $2}')  
  26.     fi  
  27.       
  28.     # run start tomcat  
  29.     ${tomcat_base_dir}/bin/startup.sh  
  30.     echo "$d success restart tomcat, the new pid is " >> ${log_file}  
  31.     ps -ef | grep ${pid_filt_pattern} | grep -v grep | awk '{print $2}' >> ${log_file}  
  32.     echo >> ${log_file}  
  33. }  
  34.   
  35. # first, check if the tomcat si listen on the port  
  36. if netstat -ln | grep ${tomcat_port}  
  37. then  
  38.     # init the check result file  
  39.     if [ -e ${guarder_dir}/checkResult.tmp ]  
  40.     then  
  41.         cat /dev/null > ${guarder_dir}/checkResult.tmp  
  42.     else  
  43.         touch ${guarder_dir}/checkResult.tmp  
  44.     fi  
  45.   
  46.     # try to get the check result  
  47.     wget -b -o wget.log -O ${guarder_dir}/checkResult.tmp ${check_page_url}  
  48.   
  49.     # wait 5 second to let the get check result job done.  
  50.     sleep 5  
  51.       
  52.     # check the result  
  53.     workflag=$(cat ${guarder_dir}/checkResult.tmp |grep ServerStillWorking)  
  54.     memoryflag=$(cat ${guarder_dir}/checkResult.tmp |grep LessOfMemory)  
  55.   
  56.     if [ "$workflag" == "" ]; then  
  57.     echo "$d can not found [ServerStillWorking] in the check result, try to restart tomcat ......" >> ${log_file}  
  58.     do_restart_tomcat  
  59.     elif [ "$memoryflag" == "" ]; then  
  60.         echo "$d can not found [LessOfMemory] in the check result, the tomcat server may out of memory, try to restart it ......" >> ${log_file}  
  61.     do_restart_tomcat  
  62.     fi  
  63. else  
  64.     echo "$d found the tomcat not listen on ${tomcat_port}, try to restart it ......" >> ${log_file}  
  65.     do_restart_tomcat  
  66. 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的内容

[html]  view plain copy print ?
  1. <%@ page contentType="text/html;charset=GBK"%>  
  2. <%@ page import="java.util.*"%>  
  3. <%  
  4.     out.println("<br>");  
  5.     out.println("<center><h1>");  
  6.     out.println("The Web Server is Running!<br><br>");  
  7.     out.println("</h1></center>");  
  8.     out.println("<br><br>");  
  9.     out.println("ServerStillWorking");//标记字符!  
  10.   
  11.     long maxMemory = Runtime.getRuntime().maxMemory() / 1024 / 1024; //java虚拟机能取得的最大内存  
  12.     long totalMemory = Runtime.getRuntime().totalMemory() / 1024 / 1024; //java虚拟机当前取得的内存大小  
  13.     long freeMemory = Runtime.getRuntime().freeMemory() / 1024 / 1024; //java虚拟机所占用的内存中的空闲部分  
  14.     long usedMemory = totalMemory - freeMemory; //java虚拟机当前实际使用的内存大小  
  15.     out.println("<br><br>Max Momery is: " + maxMemory + "M");  
  16.     out.println("<br>Total Memory is: " + totalMemory + "M");  
  17.     out.println("<br>Used Memory is: " + usedMemory + "M");  
  18.     out.println("<br>Free Memory is: " + freeMemory + "M");  
  19.     out.println("<br><br>");  
  20.   
  21.     if (usedMemory < maxMemory) {  
  22.         out.println("LessOfMemory"); //标记字符!  
  23.     } else {  
  24.         out.println("OutOfMemory"); //标记字符!  
  25.     }  
  26.     out.println("<br><br>");  
  27.     out.println(new java.util.Date());  
  28.     out.println("<br>");  
  29.     out.println(TimeZone.getDefault().getDisplayName());  
  30. %>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值