add tomcat to auto start in centos VPS

This article assumes that you have already installed the Apache Tomcat Server on your Linux Centos machine. If you need help with your with Apache Tomcat Setup then please click on the Link below.

 

Step 1: Create a file named tomcat6 in your /etc/init.d directory

$ cd /etc/init.d
$ gedit tomcat6 

If this file is created in windows, make sure the file be converted to UNIX format.  Otherwise,  following would happen:

/bin/bash^M: bad interpreter: No such file or directory

Below is one way to do the conversion in Notepad++

Step 2: Copy the following script and save the file

#!/bin/bash
# chkconfig: 2345 80 20
# Description: Tomcat Server basic start/shutdown script
# /etc/init.d/tomcat6 -- startup script for the Tomcat 6 servlet engine

TOMCAT_HOME=/usr/local/apache-tomcat-6.0.35/bin START_TOMCAT=/usr/local/apache-tomcat-6.0.35/bin/startup.sh STOP_TOMCAT=/usr/local/apache-tomcat-6.0.35/bin/shutdown.sh

start() {
 echo -n "Starting tomcat6: "
 cd $TOMCAT_HOME
 ${START_TOMCAT}
 echo "done."
}

stop() {
 echo -n "Shutting down tomcat6: "
 cd $TOMCAT_HOME
 ${STOP_TOMCAT}
 echo "done."
}

case "$1" in

start)
 start
 ;;

stop)
 stop
 ;;

restart)
 stop
 sleep 10
 start
 ;;

*)
 echo "Usage: $0 {start|stop|restart}"

esac
exit 0

Change the settings for the chkconfig based on your requirements. In a scenario where Apache Web server is in the front of Tomcat server with MySQL database usually the startup sequence should be MySQL then Tomcat and Apache Web Server in last. Also make sure you change the TOMCAT_HOME, START_TOMCAT and STOP_TOMCAT variables based on your Tomcat Install.

Step 3: Update the file permissions to make it executable by any user

$ chmod 755 tomcat6

Step 4: Make sure you have chkconfig command installed

$ chkconfig --help

otherwise just install it
$ sudo apt-get install chkconfig

 

Step 5: Run chkconfig command to add the script to the startup services

$ chkconfig --add tomcat6

(double dash before add )

Basically the chkconfig command automatically adds the symbolic links for starting and stopping the service based on the paramaters passed to it. Here is the list of links created from the above link. You can run the find command to check.

关于 chkconfig
$ find . -name “*tomcat6”

Response from the above command …
./rc.d/init.d/tomcat6
./rc.d/rc4.d/S80tomcat6
./rc.d/rc3.d/S80tomcat6
./rc.d/rc6.d/K20tomcat6
./rc.d/rc5.d/S80tomcat6
./rc.d/rc2.d/S80tomcat6
./rc.d/rc0.d/K20tomcat6
./rc.d/rc1.d/K20tomcat6

Step 6: Make sure scripts got added to the startup services

Type the following command on the Terminal

$ chkconfig --list tomcat6 

   (double dash before list)
Response should be something like this
tomcat6         0:off   1:off   2:on    3:on    4:on    5:on    6:off
 

Step 7: Verify your service is working

To start the Tomcat server
$ service tomcat6 start
To stop the Tomcat server
$ service tomcat6 stop

Advertisements

2 comments

  1. Claude · August 15, 2012

    Why would you do all this manually?

    When you install Tomcat6, and I am hoping you used a package manager to do so, you end up with all the scripts necessary for your O/S.

    Look into the chkconfig command. It will let you add a service to the startup scripts and set it to automatically start on boot, for example:

    To check to see if the service has been added to the startup scripts:
    sudo /sbin/service chkconfig –list | grep tomcat

    To add a service, tomcat6 in this case, to the startup scripts:
    sudo /sbin/service chkconfig –add tomcat6

    To set a service to start up on boot:
    sudo /sbin/service chkconfig tomcat6 on

    To set a service to not start up on boot:
    sudo /sbin/service chkconfig tomcat6 off

    To see if a service is running:
    sudo /sbin/service tomcat6 status

    To start a service:
    sudo /sbin/service tomcat6 start

    To stop a service:
    sudo /sbin/service tomcat6 stop

  2. mageru · January 25, 2013

    Thanks for the post. Worked wonderfully. Not sure what the other person thinks you get from a package manager for a tomcat install. Tomcat is a stand alone app server that only needs java to run. Silly to bother with a package manager.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s