[How To] Automatically start Tomcat Service on boot in RHEL/CentOS/Ubuntu

The Apache Tomcat is widely use in the Application servers. Sometimes, naive users are not aware of starting/stopping tomcat service. Also, Apache Tomcat service auto start is required in case of server reboot.

To achieve this, Apache Tomcat should be added in the desired runlevel with a script, as explained in the below steps.

1) Create script name tomcat containing following code

#!/bin/sh
 
# Created by: Abhijit Sandhan
# Purpose: Start or stop the Tomcat service.
 
# Check the path of Tomcat and set enviorment variables as follows in the .bashrc profile
# export CATALINA_HOME="/usr/local/tomcat7/apache-tomcat-7.0.37"
# export CATALINA_BASE="/usr/local/tomcat7/apache-tomcat-7.0.37"
# export JAVA_HOME="/usr/local/java/jdk1.7.0_17"
 
export JAVA_HOME=/usr/local/java/jdk1.7.0_17
 
case $1 in
start)
cd /usr/local/tomcat7/apache-tomcat-7.0.37/bin/
./startup.sh
;;
stop)
cd /usr/local/tomcat7/apache-tomcat-7.0.37bin/
./shutdown.sh
;;
restart)
cd /usr/local/tomcat7/apache-tomcat-7.0.37/bin/
./shutdown.sh
cd /usr/local/tomcat7/apache-tomcat-7.0.37/bin/
./startup.sh
;;
esac
exit 0

Make sure to replace the tomcat, java directory paths & variables according to  your server settings in the above script.

2) Copy the tomcat script under /etc/init.d directory.

3) Create symlinks to start/stop tomcat service for desired runlevel.

Example: For runlevel 2

 sudo ln -s /etc/init.d/tomcat /etc/rc2.d/S99tomcat

4) Reboot the server to verify if following commands are working or not

To start:

/etc/init.d/tomcat start

To stop:

/etc/int.d/tomcat stop

To restart:

/etc/init.d/tomcat restart

Last but not least, depending on your OS make sure Tomcat service starts on server boot.

Hope this helps!

Share on Facebook0Share on Google+0Tweet about this on TwitterShare on StumbleUpon0Share on LinkedIn0Share on Tumblr0Pin on Pinterest0Email this to someonePrint this page

About Abhijit Sandhan

Loves Open Source, Blogging, Traveling, Hiking and sharing Knowledge!

3 comments

  1. I just thought I might add that this script worked wonders for me after I discovered a small error. It was working as a service when I added it to RH using chkconfig, but would not start automatically on boot. The cd command didn't seem to work in the script on boot but once I removed the directory change and just opened the script itself, it worked like a charm.

  2. Why is S99 added in this command:

    sudo ln -s /etc/init.d/tomcat /etc/rc2.d/S99tomcat

    Also, I am not sure where to put the "/etc/init.d/tomcat start" start-command, in which file should i put this so that the tomcat server starts on server reboot.

Leave a Reply

Your email address will not be published. Required fields are marked *