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

The Apache Tomcat is widely use as an Web Application server. Sometimes, naive users are not aware how to start / stop / restart Apache Tomcat service on the server. Also, the Apache Tomcat service is required to auto start in case of server reboot and to avoid any manual intervention.

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

Apache Tomcat service Start , Shutdown , Restart script for RHEL , CentOS  & Ubuntu

Step 1. Create script with name tomcat containing following code

#!/bin/sh
####################################################################
# Created by: Abhijit Sandhan
# Purpose: Start/Stop/Restart Apache Tomcat service
####################################################################
# Check the path of Tomcat and set environment 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
 
#Use Case statement to start / stop /reset tomcat service
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

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

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

cp tomcat /etc/init.d/

Step 3.  Create symlinks to start/stop/restart tomcat service for desired Runlevel

Example: For Runlevel 2

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

Step 4. Reboot 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

Step 5. Now enable Apache Tomcat service to start on every server boot

For RHEL / CentOS 6.x & Ubuntu

chkconfig tomcat on

To confirm if the service is added successfully:

chkconfig --list tomcat on

For RHEL / CentOS 7.x

systemctl enable tomcat

To confirm if the service is added successfully:

systemctl status tomcat

Hope this helps!

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 *