July 27, 2004

Automatic restart tomcat when system down::[Misc]


Liang

In the Apache+tomcat clustering, tomcat server maybe down after several days running, I wrote a script to watch on the free memory and the response of the tomcat. this script will auto restart tomcat when the response is very slow and the free memory is less than 15K.

cat watch.sh

#!/bin/sh
cd /root/bin

PID=`ps -aef | grep Xms500M | grep -v grep | gawk '{print $2}'`
PID=`expr $PID + 1 - 1`

date
free
echo $PID
echo "------------------"

if [ $PID -eq 0 ]
then
sleep 10
/usr/java/tomcat/bin/startup.sh
sleep 160
fi

while [ 1 ]
do
date
free
echo "Tomcat process ID is $PID"
wget http://192.168.1.101/jsp/w_blog/blog.jsp -O working.jpg 2>> /dev/null &
sleep 120
touch working.jpg
SIZE=`du working.jpg | gawk '{print $1}'`
if [ $SIZE -le 20 ]
then


WID=`ps -aef | grep 192.168.1.101 | grep -v grep | gawk '{print $2}'`
WID=`expr $WID + 1 - 1`

if ! test -z $WID
then
killall wget
fi


echo "Tomcat restart checking......"
free
vmstat
FREEMEM=`free | grep Mem | gawk '{print $4}'`

if [ $FREEMEM -le 15000 ]
then

if [ $PID -ne 0 ]
then
kill -9 $PID
ls -l
/usr/java/tomcat/bin/shutdown.sh
fi

/usr/java/tomcat/bin/shutdown.sh
sleep 10
/usr/java/tomcat/bin/startup.sh
sleep 30
fi

date
ps -aef | grep -v httpd
ls -l
PID=`ps -aef | grep Xms500M | grep -v grep | gawk '{print $2}'`
PID=`expr $PID + 1 - 1`

fi

rm -f working.jpg

done

Posted at July 27, 2004 05:54 PM by Liang at 05:54 PM | Comments (4) | TrackBack(0) | Booso!| Niu.la收藏!


Trackback

You can ping this entry by using http://www.wespoke.com/cgi-bin/mt/mt-tb.cgi/547

Comments

第 1 楼:

Hmm,

and this script is working on your server?

PID=`ps -aef | grep Xms500M | grep -v grep | gawk '{print $2}'` produces nothing for me. If I substitute print $2 with print $1, then I will get the process ID.

And the next line produces a syntax error on my box.

Haven't checked the whole script, but you could use wget option -T (timeout in seconds) to test if you could download a test page or not and try to determine if the tomcat is still working or gone dead.

Posted by: Marcus at November 11, 2004 12:33 PM from 62.109.89.233

第 2 楼:

Hi,thanks for your comment.

I am using Linux system, which may let the "ps" command show different result.

For the expr, which is I need test the process ID, if no ID, this line will give a 0.

It depends on the system, we using this scripts for few month, and have no problem.
:)

Posted by: 6e at November 11, 2004 01:04 PM from 129.119.200.36

第 3 楼:

The first stage of a 150m investment in regional museums is praised for boosting visitor numbers...

Posted by: Luciano Wilkinson at December 14, 2006 09:05 PM from 62.150.130.26

第 4 楼:

The first stage of a 150m investment in regional museums is praised for boosting visitor numbers...

Posted by: Luciano Wilkinson at December 14, 2006 09:06 PM from 216.161.97.10

Post a comment

请注意,为了防止spam,您的留言必需含有中文字符!









Remember personal info?




所有发表