2006-09-12

How to run Tomcat automatically

To configure Linux to run Tomcat automatically at boot up, follow these steps.

First, note where you installed Java and Tomcat. My JAVA_HOME is /usr/java/jdk1.5.0, and my CATALINA_HOME is /opt/apache-tomcat. Specify these directory names in the tomcat script that you create.

Login as the tomcat user and create a script in directory $CATALINA_HOME/bin named tomcat. The script contains this text:

:
# Starts and stops Tomcat.
# Richard Kasperowski
# 2006-09-12

export JAVA_HOME=/usr/java/jdk1.5.0
export CATALINA_HOME=/opt/apache-tomcat

cd $CATALINA_HOME/bin

case "$1" in
start)
echo -n "Starting tomcat: "
su tomcat -c "./catalina.sh start"
retval=$?
echo
;;

stop)
echo -n "Shutting down tomcat: "
su tomcat -c "./catalina.sh stop"
retval=$?
echo
;;

restart)
$0 stop
$0 start
retval=$?
;;

*)
echo "Usage: tomcat {start|stop|restart}"
exit 1
esac

exit $retval

Make the script executable:

$ chmod +x tomcat

Create links from /etc/init.d and /etc/rc.d to the new script:

$ sudo ln -s /opt/apache-tomcat/bin/tomcat /etc/init.d/tomcat
$ sudo ln -s /etc/init.d/tomcat /etc/rc3.d/S99tomcat
$ sudo ln -s /etc/init.d/tomcat /etc/rc3.d/K99tomcat

Test the links:

$ sudo /etc/init.d/tomcat stop
Shutting down tomcat: Using CATALINA_BASE: /opt/apache-tomcat
Using CATALINA_HOME: /opt/apache-tomcat
Using CATALINA_TMPDIR: /opt/apache-tomcat/temp
Using JRE_HOME: /usr/java/jdk1.5.0

$ sudo /etc/init.d/tomcat start
Starting tomcat: Using CATALINA_BASE: /opt/apache-tomcat
Using CATALINA_HOME: /opt/apache-tomcat
Using CATALINA_TMPDIR: /opt/apache-tomcat/temp
Using JRE_HOME: /usr/java/jdk1.5.0

$ sudo /etc/rc3.d/K99tomcat stop
Shutting down tomcat: Using CATALINA_BASE: /opt/apache-tomcat
Using CATALINA_HOME: /opt/apache-tomcat
Using CATALINA_TMPDIR: /opt/apache-tomcat/temp
Using JRE_HOME: /usr/java/jdk1.5.0

$ sudo /etc/rc3.d/S99tomcat start
Starting tomcat: Using CATALINA_BASE: /opt/apache-tomcat
Using CATALINA_HOME: /opt/apache-tomcat
Using CATALINA_TMPDIR: /opt/apache-tomcat/temp
Using JRE_HOME: /usr/java/jdk1.5.0

$

If that all worked, you succeeded.

Resources:
The script was adapted from webMathematica configuration instructions.

No comments:

LinkWithin

Related Posts with Thumbnails