How to start and stop the Apache tomcat server in command line

Contents

Apache Tomcat Server

Apache tomcat server is a open source web server that is developed by Apache software foundation. It allows to run servlet and Java Server Pages(JSP) based web applications. It provides HTTP server environment for the Java code to run.It has the build in servlet container called Catalina in the tomcat bin directory. It handles HTTP requests such as instantiate the GET and POST method’s object .

Start the Apache tomcat server in command line

The tomcat bin directory contains all the start and shutdown script. First we need to navigate to the Apache tomcat bin directory as below. Then we can run the startup script that will start the Apache tomcat server.

$ cd /x/home/revisit_class/apache-tomcat-8.0.32/bin
Apache tomcat bin direcotry
$ sh startup.sh

Stop the Apache tomcat server in command line

There is a shutdown script in the Apache tomcat bin directory. We can run this script to stop the Apache tomcat server as below.

$ cd /x/home/revisit_class/apache-tomcat-8.0.32/bin
$ sh shutdown.sh

Check the logs in the Apache tomcat server

Once we start the Apache tomcat server, It will create lot of log files in the logs directory. The Catalina.out file is the important log file that contains our Java application logs.

$ cd /x/home/revisit_class/apache-tomcat-8.0.32/logs
$ tail -f catalina.out

Kill the process that running in port 18080

If you want to kill the process that runs on port 18080, You can get the process id by running the command as lsof -i <port number> and Kill the same using kill -9 <PID> command as below.

$ lsof -i:18080 
COMMAND   PID   USER        FD   TYPE  DEVICE SIZE/OFF NODE NAME
java    20952 revisit_class 19u  IPv6 1917219452      0t0  TCP *:18080 (LISTEN)

$ kill -9 20952

To run our Java application on Apache tomcat server

We need to set an environment variable CATALINA_HOME to the path of the directory into which we have installed Tomcat. Then we can run our application on the Apache tomcat server. The below sample script is used to run the Java application in the Apache tomcat server.

NAME=<Your application Name>
export JAVA_HOME=/user/java/latest
#CATALINA_HOME is the location of the bin files of Tomcat
export CATALINA_HOME=/x/home/revisit_class/apache-tomcat-8.0.32
#TOMCAT_USER is the default user of tomcat
export TOMCAT_USER=$(whoami)

# Set profile of our Java application
  export JAVA_OPTS="${JAVA_OPTS} -Dspring.profiles.active=prod"

  # Increase memory
  export JAVA_OPTS="${JAVA_OPTS} -Xms128m -Xmx1024m -XX:PermSize=64m -XX:MaxPermSize=256m"

  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo -e "\e[00;31m${NAME} is already running (pid: $pid)\e[00m"
  else
    # Start tomcat
    echo -e "\e[00;32mStarting ${NAME}\e[00m"
    #ulimit -n 100000
    #umask 007
    #/bin/su -p -s /bin/sh $TOMCAT_USER
        if [ `user_exists $TOMCAT_USER` = "1" ]
        then
                sh $CATALINA_HOME/bin/startup.sh
        else
                echo -e "\e[00;31m${NAME} user $TOMCAT_USER does not exists. Starting with $(id)\e[00m"
                sh $CATALINA_HOME/bin/startup.sh
        fi
        status
  fi

Recommended Articles