這一兩天現場實施的同事,發現系統的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 filetouch ${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 grepthenkill -9 $(ps -ef | grep ${pid_filt_pattern} | grep -v grep | awk '{print $2}')fi# run start tomcat${tomcat_base_dir}/bin/startup.shecho "$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 portif netstat -ln | grep ${tomcat_port}then# init the check result fileif [ -e ${guarder_dir}/checkResult.tmp ]thencat /dev/null > ${guarder_dir}/checkResult.tmpelsetouch ${guarder_dir}/checkResult.tmpfi# try to get the check resultwget -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 resultworkflag=$(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_tomcatelif [ "$memoryflag" == "" ]; thenecho "$d can not found [LessOfMemory] in the check result, the tomcat server may out of memory, try to restart it ......" >> ${log_file} do_restart_tomcatfielseecho "$d found the tomcat not listen on ${tomcat_port}, try to restart it ......" >> ${log_file}do_restart_tomcatfi
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());%>