Server Administration & Management

How to configure Tomcat to run as a Service

We here is following steps to run Tomcat as a service and create a simple Start/Stop/Restart script, as well as to start Tomcat at boot. Change to the /etc/init.d directory and create a script called ‘tomcat’ as shown below.

# cd /etc/init.d  

# vi tomcat  

# description: Tomcat Start Stop Restart  
# processname: tomcat  
# chkconfig: 234 20 80  
export JAVA_HOME  
export PATH  
case $1 in  
sh $CATALINA_HOME/bin/startup.sh  
sh $CATALINA_HOME/bin/shutdown.sh  
sh $CATALINA_HOME/bin/shutdown.sh  
sh $CATALINA_HOME/bin/startup.sh  
exit 0

The above script is simple and contains all of the basic elements you will need to get going. As you can see, we are simply calling the startup.sh and shutdown.sh scripts located in the Tomcat bin directory (/usr/local/apache-tomcat-7.0.11/bin). You can adjust your script according to your needs and, in subsequent posts, we’ll look at additional examples. CATALINA_HOME is the Tomcat home directory (/usr/local/apache-tomcat-7.0.11)

Now, set the permissions for your script to make it executable:

# chmod 755 /etc/init.d/tomcat  

We now use the chkconfig utility to have Tomcat start at boot time. In my script above, I am using chkconfig: 234 20 80. 234 are the run levels and 20 and 80 are the stop and start priorities respectively. You can adjust as needed.

# chkconfig –add tomcat  

# chkconfig –level 234 tomcat on  

Verify it:

# chkconfig –list tomcat  
tomcat          0:off   1:off   2:on    3:on    4:on    5:off   6:off


December 3, 2011 Posted by | Java, Shell Script, Tips & Tricks, Tomcat | , , , , | Leave a comment