Starting Tomcat automatically after Linux boots.

If Tomcat was not bundled as part of an operating system distribution, it won’t have the necessary file(s) to start automatically at system boot time. This describes the steps that should be taken to start Tomcat automatically after Linux boots.

1. Create a Tomcat user
Create a group, named tomcat. Create a new non-privileged user (e.g. tomcat)for Tomcat to run as. Ensure that tomcat belongs to the tomcat group. I prefer to keep this user account locked to prevent people trying to log in.

2. Change ownership of Tomcat files
Recursivly change the ownership of the tomcat installation files and directories to the new non-priviliged user
chown -R tomcat <root of tomcat installation>

For all users that need to write files to <tomcat_home>/webapps modify the group permissions to allow this write. eg:
chmod 775 <tomcat_home>/webapps

Any tomcat users should then be added to the tomcat group, allowing them to deploy their files to the webapps directory.

3. Create the Tomcat start-up script:
Create the file /etc/rc.d/init.d/tomcat

Insert the following contents, making sure that:

* JAVA_HOME references the root of the Java development kit directory
* start_tomcat is assigned to the fully qualified path to <tomcat_home>/bin/startup.sh
* stop_tomcat is assigned to the fully qualified path to <tomcat_home)/bin/shutdown.sh

File Contents

#!/bin/sh
#
# Startup script for Tomcat

JAVA_HOME=/usr/java/j2sdk1.4.1_02
export JAVA_HOME
start_tomcat=/usr/local/jakarta-tomcat-4.1.18/bin/startup.sh
stop_tomcat=/usr/local/jakarta-tomcat-4.1.18/bin/shutdown.sh

start() {
echo -n “Starting tomcat: ”
su -c ${start_tomcat} – tomcat
echo “done.”
}
stop() {
echo -n “Shutting down tomcat: ”
${stop_tomcat}
echo “done.”
}

# See how we were called
case “$1” in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 10
start
;;
*)
echo “Usage: $0 {start|stop|restart}”
esac

exit 0

4. Test the Tomcat startup script
Test the script by issuing:
[root@localhost]# . /etc/rc.d/init.d/tomcat

Output should be similar to:
Starting tomcat: Using CATALINA_BASE: /opt/tomcat
Using CATALINA_HOME: /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JAVA_HOME: /opt/java
done.

To ensure that tomcat has started try and connect to port 8080 (or whichever you configured Tomcat to use).

5. Link the script into the /etc/rc directories.

Link the script into the /etc/rc directories so that Tomcat is started and stopped when the operating system moves between runlevels. Refer to the file
/etc/inittab for runlevels, but typically Tomcat will be linked in to start on runlevel 3 (Full multi user mode, no X) and runlevel 5 (full multi user with X11). See example below.
[root@l54 rc.d]# cd /etc/rc.d/rc3.d
[root@l54 rc.d]# ln -s ../init.d/tomcat S71tomcat
[root@l54 rc.d]# ln -s ../init.d/tomcat K01tomcat
[root@l54 rc.d]# cd ../rc5.d
[root@l54 rc.d]# ln -s ../init.d/tomcat S71tomcat
[root@l54 rc.d]# ln -s ../init.d/tomcat K01tomcat

NOTE: Which number you use will depend on your local configuration, but obviously Tomcat has to start after network services! Files with an uppercase S indicate daemons that will be started. The number indicates the order in which they are started: low numbers before high. The filenames starting with K indicate daemons that will be shutdown when leaving the runlevel. Typically we like to shut down Tomcat very early in this process.

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.