Introduction
In order to Run Tomcat as a linux service, you should follow these steps:
- Install the wrapper service
- Create a tomcat environment definition script
- Create a tomcat launcher script
- Configure the wrapper service to run tomcat
- Link the tomcat launcher script to the init scripts directory
- Link the tomcat init script to the revelant init levels
Install the wrapper service
Download the wrapper service binaries for Linux (http://wrapper.tanukisoftware.org)
Copy the binaries as defined in (http://wrapper.tanukisoftware.org/doc/english/integrate-simple-nix.html):
- copy the wrapper file in the $(tomcat.home)/bin directory,
- copy the sh.script.in file in the {{$(tomcat.home)/bin directory and rename it to tomcat,
- copy the wrapper.jar file in the $(tomcat.home)/common/lib directory.
Edit the newly renamed tomcat file to ensure the following lines are defined.
APP_NAME="tomcat" APP_LONG_NAME="Tomcat Application Server"
Change the permisions of that file : chmod ug+x $(tomcat.home)/bin/tomcat)
Create a tomcat environment definition script
Create the $(server.home)/tomcat.env file with the following content (dont forget to expand the $(xxx) variables with your defined values)
CATALINA_HOME=$(tomcat.home) CATALINA_BASE=$(server.tomcat.home) export CATALINA_HOME CATALINA_BASE JAVA_HOME=$(java.home) export JAVA_HOME M2_HOME=$(m2.home) export M2_HOME PATH=$M2_HOME/bin:$CATALINA_HOME/bin:$JAVA_HOME/bin:$PATH export PATH
Create a tomcat launcher script
Create the $(server.home)/tomcat.run file with the following content (dont forget to expand the $(xxx) variables with your defined values)
#! /bin/sh
su - msu -c "cd; . ~/tomcat.env; tomcat $1"
Change the permisions of that file : chmod ug+x $(server.home)/tomcat.run)
Configure the wrapper service to run tomcat
Copy the file wrapper.conf.in from the wrapper service distribution to the $(tomcat.home)/conf directory
Ensures its content is (dont forget to expand the $(xxx) variables with your defined values)
wrapper.java.command=%JAVA_HOME%/bin/java wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperStartStopApp wrapper.java.classpath.1=$(tomcat.home)/common/lib/wrapper.jar wrapper.java.classpath.2=%JAVA_HOME%/lib/tools.jar wrapper.java.classpath.3=/$(tomcat.home)/bin/bootstrap.jar wrapper.java.classpath.4=$(tomcat.home)/bin/commons-logging-api.jar wrapper.java.library.path.1=$(tomcat.home)/common/lib wrapper.java.additional.1=-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager wrapper.java.additional.2=-Djava.util.logging.config.file=$(server.tomcat.base)/conf/logging.properties wrapper.java.additional.3=-Djava.endorsed.dirs=$(tomcat.home)/common/endorsed wrapper.java.additional.4=-Dcatalina.base=$(server.tomcat.base) wrapper.java.additional.5=-Dcatalina.home=$(tomcat.home) wrapper.java.additional.6=-Djava.io.tmpdir=$(server.tomcat.base)/temp wrapper.java.additional.7=-d64 wrapper.java.additional.8=-server wrapper.java.additional.9=-Xincgc wrapper.java.additional.10=-Xms128m wrapper.java.additional.11=-Xmx512m wrapper.java.additional.12=-Xss256m wrapper.java.additional.13=-XX:MaxPermSize=256m wrapper.app.parameter.1=org.apache.catalina.startup.Bootstrap wrapper.app.parameter.2=1 wrapper.app.parameter.3=start wrapper.app.parameter.4=org.apache.catalina.startup.Bootstrap wrapper.app.parameter.5=true wrapper.app.parameter.6=1 wrapper.app.parameter.7=stop wrapper.console.format=PM wrapper.console.loglevel=INFO wrapper.logfile=../logs/wrapper.log wrapper.logfile.format=LPTM wrapper.logfile.loglevel=INFO wrapper.logfile.maxsize=0 wrapper.logfile.maxfiles=0 wrapper.syslog.loglevel=NONE wrapper.console.title=@app.long.name@ wrapper.ntservice.name=@app.name@ wrapper.ntservice.displayname=@app.long.name@ wrapper.ntservice.description=@app.description@ wrapper.ntservice.dependency.1= wrapper.ntservice.starttype=AUTO_START wrapper.ntservice.interactive=false
As a tip to ensure Tomcat is not using too much memory, remove the default applications from your tomcat and only keep the tomcat manage servlet (with a properly defined user)
Link the tomcat launcher script to the init scripts directory
To perform this action you should be logged as root.
Execute the command ln -s $(server.home)/tomcat.run /etc/init.d/tomcat.
Ensure Tomcat can be run with /etc/init.d/tomcat start.
Verify Tomcat is running with ps axfu | grep tomcat. The tomcat process should be run by MSU
Test Tomcat by pointing your web browser to http://localhost:8080/
Link the tomcat init script to the revelant init levels
To perform this action you should be logged as root.
Execute the commands ln -s /etc/rc3.d/S99tomcat /etc/init.d/tomcat and ln -s /etc/init.d/K99tomcat /etc/init.d/tomcat
Verify with the same method as above after rebooting your server to ensure tomcat is run as a service.
