Showing posts with label Tomcat. Show all posts
Showing posts with label Tomcat. Show all posts

Friday, June 12, 2015

How to install apache tomcat as daemon service in Linux environment

Open the terminal and type the below command.
sudo nano /etc/init.d/tomcat

Paste the below script and save the file.

#!/bin/bash
#
# Author : Sree Choudary
# Copyright (c) 2k15
#
# Make kill the tomcat process
#
TOMCAT_HOME=/usr/local/tomcat/apache-tomcat-8.0.23
SHUTDOWN_WAIT=5

tomcat_pid() {
  echo `ps aux | grep org.apache.catalina.startup.Bootstrap | grep -v grep | awk '{ print $2 }'`
}

start() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ] 
  then
    echo "Tomcat is already running (pid: $pid)"
  else
    # Start tomcat
    echo "Starting tomcat"
    /bin/sh $TOMCAT_HOME/bin/startup.sh
  fi

  return 0
}

stop() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo "Stoping Tomcat"
    /bin/sh $TOMCAT_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 "\nwaiting for processes to exit";
      sleep 1
      let count=$count+1;
    done

    if [ $count -gt $kwait ]; then
      echo -n -e "\nkilling processes which didn't stop after $SHUTDOWN_WAIT seconds"
      kill -9 $pid
      echo  " \nprocess killed manually"
    fi
  else
    echo "Tomcat is not running"
  fi

  return 0
}
pid=$(tomcat_pid)

 if [ -n "$pid" ]
  then
    echo "Tomcat is running with pid: $pid"
    stop
  else
    echo "Tomcat is not running"
    start
  fi
exit 0

sudo chmod 755 /etc/init.d/tomcat
update-rc.d tomcat defaults

Finally you can start and stop tomcat using below commands.

service tomcat start
service tomcat stop

Saturday, April 27, 2013

In addition to the ability to execute Manager commands via HTTP requests. Tomcat includes a convenient set of Task definitions for the Ant (version 1.4 or later) build tool. In order to use these commands, you must perform the following setup operations:

  • Download the binary distribution of Ant from http://ant.apache.org. You must use version 1.4 or later.
  • Install the Ant distribution in a convenient directory (called ANT_HOME in the remainder of these instructions).
  • Copy the file server/lib/catalina-ant.jar from your Tomcat installation into Ant's library directory ($ANT_HOME/lib).
  • Add the $ANT_HOME/bin directory to your PATH environment variable.
  • Configure at least one username/password combination in your Tomcat user database that includes the manager-script role.
To use custom tasks within Ant, you must declare them first with a <taskdef> element. Therefore, your build.xml file might look something like this:
build.xml

<project name="MyApp" default="deploy" basedir=".">
<!-- loading properties file. -->
<property file="tomcatdeploy.properties"/>
<property name="src" location="${basedir}/src"/>
<property name="webcontent" location="${basedir}/WebContent"/>
<property name="lib" location="${webcontent}/WEB-INF/lib"/>
<!-- Configure the directory into which the web application is built -->
<property name="build" value="${basedir}/build"/>
<property name="war" location="${basedir}/war"/>
<!-- Configure the context path for this application -->
<property name="path" value="/myapp"/>
<!-- Configure properties to access the Manager application -->
<property name="url" value="${manager.url}"/>
<property name="username" value="${manager.username}"/>
<property name="password" value="${manager.password}"/>

<!-- Configure the custom Ant tasks for the Manager application -->
<taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask"/>
<taskdef name="list" classname="org.apache.catalina.ant.ListTask"/>
<taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask"/>
<taskdef name="findleaks" classname="org.apache.catalina.ant.FindLeaksTask"/>
<taskdef name="resources" classname="org.apache.catalina.ant.ResourcesTask"/>
<taskdef name="start" classname="org.apache.catalina.ant.StartTask"/>
<taskdef name="stop" classname="org.apache.catalina.ant.StopTask"/>
<taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask"/>

<path id="libpath">
<fileset dir="${lib}">
<include name="*.jar"/>
</fileset>
</path>

<target name="clean" description="Deleting build and war directories">
<delete dir="${build}"/>
<delete dir="${war}"/>
</target>

<target name="init" depends="clean" description="Creating build and war directories">
<mkdir dir="${build}"/>
<mkdir dir="${war}"/>
</target>

<target name="compile" depends="init" description="Compile web application">
<copy todir="${build}/">
<fileset dir="${webcontent}"/>
</copy>
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}/WEB-INF/classes" classpathref="libpath" includeantruntime="true"/>
</target>

<target name="war" depends="compile" description="generate the distribution war" >
<jar jarfile="${war}${path}.war" basedir="${build}"/>
</target>

<target name="deploy" description="Install web application" depends="war">
<deploy url="${url}" username="${username}" password="${password}" path="${path}" war="file:${war}${path}.war"/>
</target>

<target name="reload" description="Reload web application">
<reload  url="${url}" username="${username}" password="${password}" path="${path}"/>
</target>

<target name="undeploy" description="Remove web application">
<undeploy url="${url}" username="${username}" password="${password}" path="${path}"/>
</target>

</project>

tomcatdeploy.properties


manager.url = http://localhost:8080/manager/text
manager.username = tomcatusername
manager.password = tomcatpassword



Donot forget to give your comments /suggestions and refer to your friends.