A Funny Java Flavoured Look at the World

Monday, September 04, 2006

Auto Starting Tomcat on Linux

I have been looking at Auto Starting Tomcat on Linux the final piece of my adventures with Tomcat and Linux. The other parts of my Linux Journey are

Idiots guide to Installing Java 5 and Linux

installing two versions of Tomcat and finding out what ports are being used

Increasing the memory allocation for Tomcat

Anyway so back to autostarting Tomcat on Linux. I have looked at various articles telling me how to do it and I have basically taken the best bits of a few them. Here are the more helpful ones

http://www.sitepoint.com/article/jsp-quick-start-guide-linux/3

http://www.option-c.com/xwiki/Jakarta_Tomcat#Auto_Startup

http://www.informit.com/articles/printerfriendly.asp?p=336708&rl=1

I read the articles above and then went my own way stealing a bit from each of them

My understanding of Linux is very poor as you can guess by reading this document. Basically to auto start tomcat you need to copy a file into a directory where Linux will look for it on startup. I believe Linux looks in the directory

/ect/rc.d/

in here there are a number of number directories from 0 to 6 and these are run in numerical order with 0 being the highest priority. It seems to be standard practise to put your actual shell script into the folder

/etc/rc.d/init

Then you create a link file into the other relevant directories and then this file is automatically run when Linux starts. Let’s start by creating the file we are going to run. You could probably put the Catalina.sh file in here but I preferred to create a new one called tomcat, below is the tomcat shell script I created. You make put this into text editor and save it as tomcat into the /etc/rc.d/init directory.


#!/bin/sh
#
# Startup script for Tomcat
#

case "$1" in
start)
echo -n "Starting Tomcat "
JAVA5_HOME="/usr/java/jdk1.5.0_07" ;
JAVA_HOME=/usr/java/j2sdk1.4.2_04;
JRE_HOME=/usr/java/jdk1.5.0_07;
CATALINA_HOME=/usr/local/tomcat;
export JAVA5_HOME && export JAVA_HOME && export JRE_HOME && /usr/local/tomcat/bin/startup.sh
;;
stop)
echo -n "Stopping Tomcat "
JAVA5_HOME="/usr/java/jdk1.5.0_07" ;
JAVA_HOME=/usr/java/j2sdk1.4.2_04;
JRE_HOME=/usr/java/jdk1.5.0_07;
CATALINA_HOME=/usr/local/tomcat;
export JAVA5_HOME && export JAVA_HOME && export JRE_HOME && /usr/local/tomcat/bin/shutdown.sh ;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac

exit 0


I'm not exactly sure I need to create the JAVA5_HOME, JAVA_HOME, JRE_HOME, CATALINA_HOME but I know the Catalina.sh file uses these parameters and that file is usually called on startup so I thought I would play it safe. The start and the stop code as you can see calls the startup.sh and the shutdown.sh and this is pointing to my tomcat installation directory. I have the JAVA5_HOME because I have OC4J installed which uses the JAVA_HOME directory so I had to create a different variable.

Once I have created this file and named it tomcat, I then need to create the file links and place them in the correct directories and below is the instructions for that which are run inside a Linux terminal.

# chmod +x /etc/init.d/tomcat
# ln -s /etc/init.d/tomcat /etc/rc0.d/K91tomcat
# ln -s /etc/init.d/tomcat /etc/rc1.d/K91tomcat
# ln -s /etc/init.d/tomcat /etc/rc2.d/S91tomcat
# ln -s /etc/init.d/tomcat /etc/rc3.d/S91tomcat
# ln -s /etc/init.d/tomcat /etc/rc4.d/S91tomcat
# ln -s /etc/init.d/tomcat /etc/rc5.d/S91tomcat
# ln -s /etc/init.d/tomcat /etc/rc6.d/K91tomcat

This creates the file links which will be run automatically by the Linux system on startup and shutdown. You should check the file links have been creating correctly by going into the directories specified above and looking for the relevant files and making sure the file doesn't say link broken. Hopefully when you restart the machine tomcat will automatically be up and running.

2 Comments:

  • It is much easier if you put this at the top of your startup script:

    #
    # Startup script for tomcat
    #
    # chkconfig: 345 92 16
    #

    And then run the following command (this works on most RedHat compatible versions of Linux):

    chkconfig --add tomcat

    It will create the needed boot and shutdown links for you. Try "whereis chkconfig" if you get a command not found error. If that returns nothing, you have to make the links manually :(

    Also, I'm pretty sure that you don't need to set JAVA5_HOME and JRE_HOME -- just set JAVA_HOME to the JDK 1.5.0 location and it will find where everything is.

    By Anonymous Anonymous, at Tue Sept 05, 10:20:00 pm 2006  

  • thanks for the hint it looks like that will save me a bit of typing in the future.

    In the exam I use I can't use the JAVA_HOME because something else is using that and it is using a different version of Java.

    By Blogger The Hosk, at Wed Sept 06, 08:24:00 am 2006  

Post a Comment

<< Home