Application ServerTomcat

[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 run level with a custom script as explained in the below steps.

Apache Tomcat service Start , Shutdown , Restart script for RHEL , CentOS, Fedora & 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 & installations 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 Run level

Example: For Run level 2

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

There are 6 stages of Linux boot process where 5th is INIT and 6th is RunLevel. When RunLevel stage is executed, the scripts are executed in ascending order. The script starting with a 'K' is called with a 'stop' argument and script starting with a 'S' is called with a 'start' argument. All 'K' scripts are executed before any 'S' script. The S script with S99 means it will start after S98, S97, 96... etc. It is basically use to adhere the dependency between two services. In short, lower the number higher is the priority.

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 server re-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!

8 Comments

  1. 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.

  2. 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.

      1. HI . above script not working properly. i tried number of times, but no result. tomcat service starting by the script , but in browser tomcat not appearing. complete blank.

        could you help with new script if you have.
        thanks and reg
        ravindra

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top button