July 27, 2004
Automatic restart tomcat when system down::[Misc]

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
Trackback
You can ping this entry by using http://www.wespoke.com/cgi-bin/mt/mt-tb.cgi/547
Comments
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.233Hi,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.
:)
