Tomcat

How to Start and Restart Tomcat Server as a Service

Apache Tomcat is a web server and servlet container that is used to serve Java applications. A servlet is a Java technology-based Web component, managed by a container, that generates dynamic content.

1. The tools

  • Debian based Linux distribution
  • Microsoft Windows
  • Java JDK
  • Apache Tomcat

2. Introduction

When you download Apache Tomcat binary distribution, you need to unzip the folder. Every time you are going to use the server, you need to start and stop its service manually.

Most of the time, is needed to start the server with the system. To start/stop/restart the server, Tomcat provide some utilities. In this example we are going to install tomcat as a service and use the utilities provided by Tomcat to start/stop/restart the tomcat service.

3. Prerequisites

  • JDK installed

4. Download Tomcat

Go to the page

1 Download Tomcat for Windows
1 Download Tomcat for Windows

Download the tomcat server as a tar.gz compressed file for Linux.

2 Download Tomcat for Linux
2 Download Tomcat for Linux

5. Tomcat in Windows

5.1 Unzip Tomcat

Unzip the tomcat downloaded zip in the folder you want to have it.

3 Unzip tomcat windows
3 Unzip tomcat windows

5.2 Open the terminal console

Open the terminal and go to the folder where Tomcat is unzipped.

4 Tomcat folder
4 Tomcat folder

5.3 Install service

go to the bin folder and type the command:

Install service

C:\Java\Apache Tomcat 8.0.27\bin>service install

This command install the Tomcat service in Windows. You should get the following output

Install service output

Installing the service 'Tomcat8' ...
Using CATALINA_HOME:    "C:\Java\Apache Tomcat 8.0.27"
Using CATALINA_BASE:    "C:\Java\Apache Tomcat 8.0.27"
Using JAVA_HOME:        "C:\Java\jdk1.8.0_40"
Using JRE_HOME:         "C:\Java\jdk1.8.0_40\jre"
Using JVM:              "C:\Java\jdk1.8.0_40\jre\bin\server\jvm.dll"
The service 'Tomcat8' has been installed.

5.4 Check service

You can check if the service is properly installed with the windows commands for services. Type the command

Check service

C:\Java\Apache Tomcat 8.0.27\bin>sc query Tomcat8

You should get an output similar to this:

sc query output

SERVICE_NAME: Tomcat8
        TYPE                         : 10  WIN32_OWN_PROCESS
        STATUS                       : 1  STOPPED
        WIN32_OUTPUT_CODE    		 : 1077  (0x435)
        SERVICE_OUTPUT_CODE	         : 0  (0x0)
        CONTROL_POINT                : 0x0
        WAIT                         : 0x0

When you install the service, the service is stopped.

5.5 Start service

To start the service type this command:

Start tomcat service

C:\Java\Apache Tomcat 8.0.27\bin>sc start tomcat8

You should get an output similar to this:

Start service output

SERVICE_NAME: tomcat8
        TYPE               : 10  WIN32_OWN_PROCESS
        STATUS             : 2  START_PENDING
                                (NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
        WIN32_OUTPUT_CODE  : 0  (0x0)
        SERVICE-OUTPUT_CODE: 0  (0x0)
        COMPROB_POINT      : 0x0
        INITIAL-STATUS     : 0x7d0
        PID                : 908
        MARKS              :

It takes a while to start the service. Note that the status starts pending. You can check the service status with:

Check service

C:\Java\Apache Tomcat 8.0.27\bin>sc query Tomcat8

Wait until the service is started.

sc query output

SERVICE_NAME: Tomcat8
        TYPE                         : 10  WIN32_OWN_PROCESS
        STATUS                       : 3 STARTED
        WIN32_OUTPUT_CODE    		 : 1077  (0x435)
        SERVICE_OUTPUT_CODE	         : 0  (0x0)
        CONTROL_POINT                : 0x0
        WAIT                         : 0x0

Open the URL http://localhost:8080 in the browser and check that Tomcat is working. You should get the Tomcat Welcome screen:

5 Tomcat Welcome
5 Tomcat Welcome

5.6 Stop service

To stop the Tomcat service type this command:

Stop tomcat service

C:\Java\Apache Tomcat 8.0.27\bin>sc stop tomcat8

You should get an output similar to this:

Stop service output

NOMBRE_SERVICIO: tomcat8
        TYPE               : 10  WIN32_OWN_PROCESS
        STATUS             : 3  STOP_PENDING
                                (NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
        WIN32_OUTPUT_CODE  : 0  (0x0)
        SERVICE-OUTPUT_CODE: 0  (0x0)
        COMPROB_POINT      : 0x0
        INITIAL-STATUS     : 0x7d0
        PID                : 908
        MARKS              :

It takes a while to stop the service. Note the status STOP_PENDING. You can check the service status with:

Check service

C:\Java\Apache Tomcat 8.0.27\bin>sc query Tomcat8

Wait until the service is stopped.

5.7 Autostart service

If you need to start the service when Windows start, type the following command:

Autostart service

C:\Java\Apache Tomcat 8.0.27\bin>sc config Tomcat8 start= auto

You should get an output similar to this:

Autostart service output

[SC] ChangeServiceConfig OK

Now restart the computer and check that Tomcat is starting when the system starts.

5.8 Remove service

If you need to remove the service, type the following command:

Remove service

C:\Java\Apache Tomcat 8.0.27\bin>service remove

And you should get the following output:

Remove service output

Removing the service 'Tomcat8' ...
Using CATALINA_BASE:    "C:\Java\Apache Tomcat 8.0.27"
The service 'Tomcat8' has been removed

6. Tomcat in Linux

Every Linux distribution has its own way to manage services. This procedure should work in deb based distributions. We are going to make this on Debian 8 Linux distribution.

6.1 Uncompress the tomcat folder

Open your Linux terminal.

6 Linux terminal
6 Linux terminal

Work as a superuser. Type the following command:

Superuser login
su
write your superuser password.

Copy the tomcat compressed file to the /opt directory.

Copy compressed file

cp /path_to_file/tomcat8.tar.gz /opt

go to the /opt directory.

Go to opt

cd /opt

Uncompress the tomcat file

Uncompress tomcat

tar -zxvf tomcat8.tar.gz

6.2 Create the service script

To work with tomcat as a service in Linux, we are going to create a script for this purpose

Init scripts folder

cd /etc/init.d

In this folder create the file tomcat and give the appropriate permissions to the file.

Script permissions

chown tomcat:tomcat tomcat8
chmod 755 tomcat8

Edit the file with your favorite editor.

Edit script

vi tomcat8

6.3 Script content

6.3.1 Script header

Script header

#!/bin/bash

### BEGIN INIT INFO
# Provides:          tomcat8
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Apache Tomcat
# Description:       Open source web server and Servlet container
### END INIT INFO

The header of the script is very important because it  is used by the system to properly execute the script.

#!/bin/bash Indicates that this is a bash script.
### BEGIN INIT INFO This mark the beginning of the header.

# Provides: tomcat8 Indicates what is provided by the script.
# Required-Start: $remote_fs $syslog Runlevels required to start.

# Default-Start: 2 3 4 5 Default start runlevels.
# Default-Stop: 0 1 6 Default stop runlevels.

# Short-Description: Apache Tomcat A short description
# Description: Open source web server and Servlet container A long description
### END INIT INFO Mark the end of the header.

6.3.2 Initialize variables

Initialization variables

export JAVA_HOME=/usr/lib/jvm/java-8-oracle
export PATH=$JAVA_HOME/bin:$PATH
export CATALINA_HOME=/opt/apache-tomcat-8.0.33
export CATALINA_BASE=/opt/apache-tomcat-8.0.33
TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}"
SHUTDOWN_WAIT=20

export JAVA_HOME=/usr/lib/jvm/java-8-oracle JAVA_HOME is the directory where the JDK is located.
export PATH=$JAVA_HOME/bin:$PATH The bin JAVA_HOME is exported to the PATH to use it everywhere.

export CATALINA_HOME=/opt/apache-tomcat-8.0.33 Location of Tomcat directory.
export CATALINA_BASE=/opt/apache-tomcat-8.0.33 Location of Tomcat directory.

TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}" Feedback message showed when you run the script without parameters
SHUTDOWN_WAIT=20 Waiting time to shutdown the server, to allow the server finish any pending tasks.

6.3.3 Print tomcat pid function

Tomcat PID

tomcat_pid() {
        echo `ps -fe | grep $CATALINA_BASE | grep -v grep | tr -s " "|cut -d" " -f2`
}

This function prints the tomcat process id using some terminal utilities.

echo `ps -fe | grep $CATALINA_BASE | grep -v grep | tr -s " "|cut -d" " -f2`
prints the tomcat process id.

6.3.4 Print tomcat status function

Status function

status(){
          pid=$(tomcat_pid)
          if [ -n "$pid" ]; then echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m"
          else echo -e "\e[00;31mTomcat is not running\e[00m"
          fi
}

pid=$(tomcat_pid) Gets the tomcat pid.
if [ -n "$pid" ]; then echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m" If the pid exists then tomcat is running.
else echo -e "\e[00;31mTomcat is not running\e[00m" Prints the tomcat is not running.

6.3.5 Start tomcat function

Start function

start() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m"
  else
    echo -e "\e[00;32mStarting tomcat\e[00m"
    sh $CATALINA_HOME/bin/startup.sh
    status
  fi
  return 0
}

This function is running when you run the script with the start parameter.

pid=$(tomcat_pid) Gets the tomcat pid
if [ -n "$pid" ] If the pid exists.

echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m" The tomcat server is running.
echo -e "\e[00;32mStarting tomcat\e[00m" If the tomcat server is not running print the message that tomcat is starting.

sh $CATALINA_HOME/bin/startup.sh Run the startup script provided by tomcat.
status Print the status.

return 0 Return without errors.

6.3.6 Stop tomcat function

Stop function

stop() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo -e "\e[00;31mStoping Tomcat\e[00m"
        sh $CATALINA_HOME/bin/shutdown.sh

    let kwait=$SHUTDOWN_WAIT
    count=0;
    until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
    do
      echo -n -e "\n\e[00;31mwaiting for processes to exit\e[00m";
      sleep 1
      let count=$count+1;
    done

    if [ $count -gt $kwait ]; then
      echo -n -e "\n\e[00;31mkilling processes which didn't stop after $SHUTDOWN_WAIT seconds\e[00m"
      kill -9 $pid
    fi
  else
    echo -e "\e[00;31mTomcat is not running\e[00m"
  fi

  return 0
}

This function stops the Tomcat server

pid=$(tomcat_pid) Gets the Tomcat PID
if [ -n "$pid" ] If the PID doesn’t exist
echo -e "\e[00;31mStoping Tomcat\e[00m" Prints the message that Tomcat is stopping

sh $CATALINA_HOME/bin/shutdown.sh Runs the script provided by tomcat to stop the server.
let kwait=$SHUTDOWN_WAIT Gets the timeout variable.

count=0; Initializes a counter.
until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ] Checks if the pid still exist.

echo -n -e "\n\e[00;31mwaiting for processes to exit\e[00m"; Prints a waiting message.
sleep 1 Sleeps.

let count=$count+1; Updates the counter.
if [ $count -gt $kwait ]; then if the counter is equal to the waiting time.

echo -n -e "\n\e[00;31mkilling processes which didn't stop after $SHUTDOWN_WAIT seconds\e[00m" Prints a message that the server didn’t stop before the waiting time.
kill -9 $pid Forces to quit the server process.

echo -e "\e[00;31mTomcat is not running\e[00m" Prints a message that the server is not running.
return 0 Returns without errors.

6.3.7 Script main body

When we run the script the parameters are selected using a case statement, to choose the right option.

Script main body

case $1 in
        start)
          start
        ;;
        stop)  
          stop
        ;;
        restart)
          stop
          start
        ;;
        status)
          status
        ;;
        *)
          echo -e $TOMCAT_USAGE
        ;;
esac
exit 0

case $1 in Examines the script parameter.
start) If the parameter is start.

start Starts the server.
stop) If the parameter is stop.

stop Stops the server.
restart) If the parameter is restart.

stop Stops the server.
start Start the server.

status) If the parameter is status.
status Prints the status.

*) If there is no parameters other any other parameter.
echo -e $TOMCAT_USAGE Prints the help message.

esac End the case statement.
exit 0 Exits without errors.

6.4 The complete Linux source code

tomcat8

#!/bin/bash

### BEGIN INIT INFO
# Provides:          tomcat8
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Apache Tomcat
# Description:       Open source web server and Servlet container
### END INIT INFO

export JAVA_HOME=/usr/lib/jvm/java-8-oracle
export PATH=$JAVA_HOME/bin:$PATH
export CATALINA_HOME=/opt/apache-tomcat-8.0.33
export CATALINA_BASE=/opt/apache-tomcat-8.0.33
TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}"
SHUTDOWN_WAIT=20

tomcat_pid() {
        echo `ps -fe | grep $CATALINA_BASE | grep -v grep | tr -s " "|cut -d" " -f2`
}

start() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m"
  else
    echo -e "\e[00;32mStarting tomcat\e[00m"
    sh $CATALINA_HOME/bin/startup.sh
    status
  fi
  return 0
}

status(){
          pid=$(tomcat_pid)
          if [ -n "$pid" ]; then echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m"
          else echo -e "\e[00;31mTomcat is not running\e[00m"
          fi
}

stop() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo -e "\e[00;31mStoping Tomcat\e[00m"
        sh $CATALINA_HOME/bin/shutdown.sh

    let kwait=$SHUTDOWN_WAIT
    count=0;
    until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
    do
      echo -n -e "\n\e[00;31mwaiting for processes to exit\e[00m";
      sleep 1
      let count=$count+1;
    done

    if [ $count -gt $kwait ]; then
      echo -n -e "\n\e[00;31mkilling processes which didn't stop after $SHUTDOWN_WAIT seconds\e[00m"
      kill -9 $pid
    fi
  else
    echo -e "\e[00;31mTomcat is not running\e[00m"
  fi

  return 0
}

case $1 in

        start)
          start
        ;;

        stop)  
          stop
        ;;

        restart)
          stop
          start
        ;;

        status)
                status

        ;;

        *)
                echo -e $TOMCAT_USAGE
        ;;

esac    
exit 0

6.5 Make the script run at boot

This command is used to make Linux aware to run the script at startup.

Run at boot

update-rc.d /etc/init.d/tomcat8

6.6 Linux results

6.6.1 Run script without arguments

We run the command without arguments

tomcat

root@debian:~# /etc/init.d/tomcat8

The script shows the help.

tomcat script output

Usage: /etc/init.d/tomcat8 {start|stop|status|restart}

6.6.2 Run script with status parameter

We run the script with the status parameter

tomcat status

root@debian:~# /etc/init.d/tomcat8 status

If the service is running we get the following output:

tomcat status running output

Tomcat is running with pid: 619

If the service is not running we get the following output:

tomcat status not running output

Tomcat is not running

6.6.3 Run script with start parameter

We run the script with the start parameter

tomcat start

root@debian:~# /etc/init.d/tomcat8 start

If the server is running we get the output

tomcat start with server running output

Tomcat is already running (pid: 3445)

If the server is not running we get the output

tomcat start with server not running output

Starting tomcat
Using CATALINA_BASE:   /opt/apache-tomcat-8.0.33
Using CATALINA_HOME:   /opt/apache-tomcat-8.0.33
Using CATALINA_TMPDIR: /opt/apache-tomcat-8.0.33/temp
Using JRE_HOME:        /usr/lib/jvm/java-8-oracle
Using CLASSPATH:       /opt/apache-tomcat-8.0.33/bin/bootstrap.jar:/opt/apache-tomcat-8.0.33/bin/tomcat-juli.jar
Tomcat started.
Tomcat is running with pid: 2715

6.6.4 Run script with stop parameter

We run the script with the stop parameter

tomcat stop

root@debian:~# /etc/init.d/tomcat8 stop

If the server is running we get the output

tomcat stop with server running output

Stoping Tomcat
Using CATALINA_BASE:   /opt/apache-tomcat-8.0.33
Using CATALINA_HOME:   /opt/apache-tomcat-8.0.33
Using CATALINA_TMPDIR: /opt/apache-tomcat-8.0.33/temp
Using JRE_HOME:        /usr/lib/jvm/java-8-oracle
Using CLASSPATH:       /opt/apache-tomcat-8.0.33/bin/bootstrap.jar:/opt/apache-tomcat-8.0.33/bin/tomcat-juli.jar

If the server is not running we get the output

tomcat stop with server not running output

Tomcat is not running

6.6.5 Run script with restart parameter

We run the script with the restart parameter

tomcat restart

root@debian:~# /etc/init.d/tomcat8 restart

If the server is running we get the output

tomcat restart with server running output

Stoping Tomcat
Using CATALINA_BASE:   /opt/apache-tomcat-8.0.33
Using CATALINA_HOME:   /opt/apache-tomcat-8.0.33
Using CATALINA_TMPDIR: /opt/apache-tomcat-8.0.33/temp
Using JRE_HOME:        /usr/lib/jvm/java-8-oracle
Using CLASSPATH:       /opt/apache-tomcat-8.0.33/bin/bootstrap.jar:/opt/apache-tomcat-8.0.33/bin/tomcat-juli.jar

waiting for processes to exit
Starting tomcat
Using CATALINA_BASE:   /opt/apache-tomcat-8.0.33
Using CATALINA_HOME:   /opt/apache-tomcat-8.0.33
Using CATALINA_TMPDIR: /opt/apache-tomcat-8.0.33/temp
Using JRE_HOME:        /usr/lib/jvm/java-8-oracle
Using CLASSPATH:       /opt/apache-tomcat-8.0.33/bin/bootstrap.jar:/opt/apache-tomcat-8.0.33/bin/tomcat-juli.jar
Tomcat started.
Tomcat is running with pid: 2874

If the server is not running we get the output

tomcat restart with server not running output

Tomcat is not running
Starting tomcat
Using CATALINA_BASE:   /opt/apache-tomcat-8.0.33
Using CATALINA_HOME:   /opt/apache-tomcat-8.0.33
Using CATALINA_TMPDIR: /opt/apache-tomcat-8.0.33/temp
Using JRE_HOME:        /usr/lib/jvm/java-8-oracle
Using CLASSPATH:       /opt/apache-tomcat-8.0.33/bin/bootstrap.jar:/opt/apache-tomcat-8.0.33/bin/tomcat-juli.jar
Tomcat started.
Tomcat is running with pid: 3571

7. Download the Source Code

This was an example of how to start and restart Tomcat server as a service.

Download
You can download the Linux script here: tomcat8

Jesus Boadas

I'm a self taught programmer, I began programming back in 1991 using an IBM A10 mainframe with Pascal an Assembler IBM 360/70 emulator and Turbo C on a X86 PC, since that I work for the banking industry with emerging technologies like Fox Pro, Visual Fox Pro, Visual Basic, Visual C++, Borland C++, lately I moved out to the Airline industry, leading designing and programming in-house web applications with Flex, Actionscript, PHP, Python and Rails and in the last 7 years I focused all my work in Java, working on Linux servers using GlassFish, TomCat, Apache and MySql.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button