Got feedback or spotted a mistake?

Leave a comment at the end of this page or email contact@krishagni.com

How to run Tomcat as a service

In this document, $TOMCAT_HOME is used to refer the absolute path of the directory containing Tomcat server resources like executable binaries (bin), configuration (conf), web applications (webapps) etc. 

The Tomcat Service for Windows

Follow the below steps to run Tomcat as a service for Windows.

  1. Open the command prompt with administrator privileges and go to directory >(TOMCAT_HOME\bin).
  2. Run command service.bat install openspecimen (This will install Tomcat as a Windows service).
  3. Go to the task manager, click on services, check for the service with the display name 'Apache Tomcat 9'.
  4. Select the service with the above name, on the left window you can start, stop and restart the service.
  5. If you want to uninstall the service run service.bat uninstall openspecimen from the same location.

The Tomcat Service for new versions of RedHat / CentOS / Ubuntu.

  1. Download openspecimen.service.

  2. Edit file downloaded in step #1 in any editor of your choice. Replace occurrence of $CATALINA_HOME and $TOMCAT_HOME with the absolute path of Tomcat directory. (Like /usr/local/openspecimen/tomcat-as)

  3. Copy edited file to /etc/systemd/system directory

  4. Stop Tomcat process, if running

  5. Execute below command to notify systemd that OpenSpecimen service has been added
    $ sudo systemctl daemon-reload

  6. Use below commands to start, status .stop, or restart OpenSpecimen service
    $ sudo systemctl start openspecimen.service
    sudo systemctl status openspecimen.service
    $ sudo systemctl stop openspecimen.service    
    $ sudo systemctl restart openspecimen.service

  7. Enable starting of OpenSpecimen on boot by executing below command:
    $ sudo systemctl enable openspecimen.service


Note:

  1. Before running point no 7 make sure openspecimen service is not in running state.
  2. In the openspecimen.service file. Value of User directive is the name of the Linux user from who will start the service. (By default value is set to openspecimen).


Error may occur when starting the service: Neither the JAVA_HOME nor the JRE_HOME environment variable is defined

Resolution : Add 'export JAVA_HOME=<java-installation-path>' line in setenv.sh file located in Tomcat/bin directory. Create a file if not already present and give executable permission to setenv.sh file

Example: export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_45


 The Tomcat Service for older version of Linux

Note: Use sudo or root privileges while creating a Tomcat service.

  1. Change directory to ${TOMCAT_HOME}/bin
    $ cd ${TOMCAT_HOME}/bin

  2. Edit 'openspecimen' file in any editor of your choice. Set the value of CATALINA_HOME variable found at top of the file to that of ${TOMCAT_HOME}

  3. The service name will be same as filename i.e. openspecimen. If you wish to change service name, rename the file and replace references to "openspecimen" used in printing debug and informational messages.

  4. Copy 'openspecimen' file to /etc/init.d directory
    $ sudo cp openspecimen /etc/init.d/

  5. Stop the Tomcat process, if running

  6. Run below command to install service and subsequently start Tomcat process
    $ sudo os_service_install.sh

Ubuntu

To enable starting OpenSpecimen service on boot, execute below command:

$ sudo update-rc.d openspecimen defaults

If you've customized service name, use the customized name instead of openspecimen.

Redhat 6.x/CentOS 6.x

Use chkconfig to enable starting OpenSpecimen service on boot

  1. Check whether 'openspecimen' service has been added by executing below command
    $ sudo /sbin/chkconfig --list openspecimen

    If the above command doesn't generate any output, it means OpenSpecimen service has not been added. Execute below command to add OpenSpecimen service
    $ sudo /sbin/chkconfig --add openspecimen

  2. Enable OpenSpecimen to start automatically on boot by executing below command
    $ sudo /sbin/chkconfig openspecimen

Use below command line to start OpenSpecimen service at a specific run level:

$ sudo /sbin/chkconfig --level <level> openspecimen


Note: To setup the 'OpenSpecimen service' we need the below prerequisites :
1) Write permissions for the directory /etc/init.d/ so that we can copy the OpenSpecimen service descriptor/script.
2) Permissions to execute the command chkconfig --add openspecimen.



Got feedback or spotted a mistake?

Leave a comment at the end of this page or email contact@krishagni.com