Article updated on

Simple Linux Service Wrapper to Run Tomcat on startup

This is a simple wrapper to run Tomcat as a service. You need to be a root user.

Intructions to run install as service an run on startup:

  1. Create a file under  /etc/init.d/   with nano or vi and paste the example script below. ex.  sudo vi /etc/init.d/tomcatXX
  2. Modify the PATH_TO_TOMCAT to your tomcat installation path and modify JAVA_HOME to your JRE/JDK path.
  3. Write the file and give execution permisions ex. sudo chmod +x /etc/init.d/tomcatXX
  4. Test that it runs ex. sudo service tomcatXX start
  5. Test that it stops ex. sudo service tomcatXX stop
  6. Test that it restarts ex. sudo service tomcatXX restart

Copy the code example:

#!/bin/sh
PATH_TO_TOMCAT=/var/lib/tomcat6.0
export JAVA_HOME=/usr/lib/jvm/java-6-openjdk
 case $1 in
   start)
      sh $PATH_TO_TOMCAT/bin/startup.sh
     ;;
 stop)
      sh $PATH_TO_TOMCAT/bin/shutdown.sh
     ;;
 restart)
      sh $PATH_TO_TOMCAT/bin/shutdown.sh
      sh $PATH_TO_TOMCAT/bin/startup.sh
     ;;
    esac
exit 0

 

Known Issues

  • If you stop the service when it is already stopped you'll get a java.net.ConnectException: Connection refused
  • You must have a bin/startup.sh and bin/shutdown.sh in your installation.
  • In Ubuntu use sudo update-rc.d tomcatXX defaults if you want to run the service when the SO starts or sudo update-rc.d tomcatXX disable to remove from startup.