Archive for the ‘Linux’ Category

Runing Tomcat as Linux service

January 21, 2007

Not big deal but I am bit tired to google for this script each time I setup new box. Now I know where to get it without spending two valuable extra minutes to google. First create /etc/rc.d/init.d/tomcatd :

#!/bin/bash  
#  
# Startup script for Tomcat  
#  
# chkconfig: 345 84 16  
# description: Tomcat jakarta JSP server  
TOMCAT_HOME=/usr/share/apache-tomcat-5.5.20  
TOMCAT_START=$TOMCAT_HOME/bin/startup.sh  
TOMCAT_STOP=$TOMCAT_HOME/bin/shutdown.sh  
#Necessary environment variables  
export JAVA_HOME=”/usr/share/jdk1.5.0_10?  
export CATALINA_HOME=”/usr/share/apache-tomcat-5.5.20?  
#export LD_KERNEL_ASSUME=”2.2.5?  
# Source function library.  
. /etc/rc.d/init.d/functions  
# Source networking configuration.  
. /etc/sysconfig/network  
# Check that networking is up.  
[ ${NETWORKING} = “no” ] && exit 0  
#Check for tomcat script  
if [ ! -f $TOMCAT_HOME/bin/catalina.sh ]  
then  
    echo “Tomcat not available…”  
    exit  
fi  
start() {  
    echo -n “Starting Tomcat: “  
    su - tomcat -c $TOMCAT_START  
    echo  
    touch /var/lock/subsys/tomcatd  
# We may need to sleep here so it will be up for apache  
    sleep 3  
#Instead should check to see if apache is up by looking for httpd.pid  
}  
stop() {  
    echo -n $”Shutting down Tomcat: “  
    su - tomcat -c $TOMCAT_STOP  
    rm -f /var/lock/subsys/tomcatd  
    echo  
}  
status() {  
    ps ax –width=1000 | grep “[o]rg.apache.catalina.startup.Bootstrap start” | awk ‘{printf $1 ” “}’ | wc | awk ‘{print $2}’ > /tmp/tomcat_process_count.txt  
   read line

Add user tomcat. Please note that it is normal user with his own home. Many times it was very useful.

adduser tomcat

Set JAVA_HOME for all users in /etc/profile.d/java.sh . When we run ’su – tomcat’ this script will set up our environment:

export JAVA_HOME=/usr/share/jdk1.5.0_10

Give enough permissions to tomacat:

chown tomcat /usr/share/apache-tomcat-5.5.20/ -R

Now you are able to run tomcat with

service tomcatd start

The last thing is to run it when system boots:

chkconfig –add tomcatd  
chkconfig tomcatd on

Done

Watchdog for tomcat

August 16, 2006

Here is just short script that restarts tomcat when it wrizes. It happens to our prod during peak load. You need to place inder webapps/ROOT directory a file isalive.html with one word ‘YES’ inside. Or alternatively modify a script and access default index.jsp.



#!/bin/sh
HOST=127.0.0.1
PORT=8080

#infinite loop
while [ 1 ]
do
	#try to access tomcat's page
	RES=`wget -O - -o /dev/null --proxy=off http://${HOST}:${PORT}/isalive.html | awk '{ print $1 }'`
	echo got ${RES}
	#decide on reply
	if [ "$RES" = "YES" ]
	then
		echo tomcat is responding on $HOST:$PORT
	else
		echo tomcat seems to be dead.
		echo Killing...
		for thepin in `ps -Af | grep -v grep | grep tomcat | grep catalina | awk '{ print $2 }'`
		do
			kill -9 ${thepin}
		done
		echo Starting...
		sudo -u tomcat /usr/local/tomcat/bin/startup.sh
	fi

sleep 60
done