Tomcat daemon in Linux to avoid Tomcat interruption or memory overflow

Source: Internet
Author: User

During the past two days, colleagues on the site discovered that the system's tomcat would be suspended due to memory overflow. Colleagues at the scene could not find the problem at the moment. The last trick was to write a script to monitor it.

1. Environment

Operating System: centos 4.8

JDK version: j2sdk1.4.2

Tomcat version: tomcat-5.0.28

2 Monitoring script

#!/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. Add a scheduled task crontab

3.1 Edit a task

[Test @ cent4 ~] $ Crontab-e

Enter the VI interface for editing crontab, press I to enter the insert mode, and copy the following code to the input interface.

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

Note: Check every 20 minutes (modifiable) and add ">/dev/null 2> & 1" to prevent it from sending emails.

3.2 save and exit

Press ESC to enter the command mode, enter WQ, and press enter to save and exit.

3.3 view tasks

Finally, check whether the task is edited successfully.

[Test @ cent4 ~] $ Crontab-l

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

4. jsp content

<% @ Page contenttype = "text/html; charset = GBK" %> <% @ page import = "Java. util. * "%> <% Out. println ("<br>"); out. println ("<center> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.