1

Is it possible to modify a certain batch file for Tomcat such that before Tomcat which is set as a web service will start would do something like checking or modifying some files?

So what we want is everytime the Tomcat Service starts, do some modifying for some specific files which is needed by the application which is hosted by tomcat.

If its possible to do it by one of the batch files in tomcat then which batch file should we modify? Any ideas would be much appreciated. By the way, we're using tomcat 7.

JŸR
  • 102
  • 2
  • 8
  • You can modify "startup.bat" which is located in bin folder. – Shashank Kadne Feb 13 '12 at 07:02
  • The problem here is when we start the Service in services.msc, it seems that startup.bat was not called since our modification was not reflected. But if startup.bat is manually clicked the modification made was reflected. – JŸR Feb 13 '12 at 07:23
  • View the properties of that service and see the executable file it calls.If its startup.bat then it should work. – Shashank Kadne Feb 13 '12 at 07:28
  • "tomcat7_x86.exe" is the file it calls. – JŸR Feb 13 '12 at 07:32
  • 1
    see if this helps...http://tomcat.10.n6.nabble.com/Running-Tomcat-as-service-is-it-possible-to-make-quot-tomcat7-exe-quot-run-a-batch-file-each-time-it-td2107227.html – Shashank Kadne Feb 13 '12 at 07:42
  • based on this link, looks like there's no light for this :D – JŸR Feb 13 '12 at 08:03
  • Yeah I kn...thats the reason y I asked you to go through it..:)....can't you start the server using a batch file ? or you can edit the registry to force the Tomcat service to start a batch file and not the excutable(tomcat.exe)...but i dnt think thats a feasible option. – Shashank Kadne Feb 13 '12 at 08:13
  • Starting the server through a batch file is perhaps the last good option but still there are cons to it. But in anyway thanks for your ideas and effort Kadne. – JŸR Feb 13 '12 at 08:21
  • you r welcome...:)...let me know which approach you choose. That will help me in future for sure...:) – Shashank Kadne Feb 13 '12 at 08:26
  • hello kadne, we've found a better approach to this. Tomcat will try to deploy this war files that's why in the configurations where those .xml configurations files are located like creating the databases specifically services-context.xml we place there to initialize our method once deployment is starting. :) – JŸR Mar 01 '12 at 03:30

1 Answers1

0

Short answer: It is NOT possible, at least there is no easy way to do it, just as it was discussed on the page Shashank Kadne pointed out. Other than starting Tomcat via startup.bat script and placing you code (or a call to your BAT file) in the setenv.bat file in the same folder as startup.bat.


If you are trying to run applications like Jenkins, Artifactory and alike on a single Tomcat installation - as discussed on that page - a better way would be to create a Tomcat Server Instance (also known as CATALINA_BASE) for each additional application.

This way you can have each application as a Windows Service and control it as a service.

Assumptions:

  • You Java is installed at C:\Program Files\Java\jdk-7.0_03.

  • You Tomcat is installed at C:\Program Files\Apache\Tomcat 7.

  • You want e.g. Jenkins server to be at C:\Program Files\My Jenkins.

Start cmd.exe, and do the following:

    C:\Users\me> set "JAVA_HOME=C:\Program Files\Java\jdk-7.0_03"
    C:\Users\me> set "CATALINA_HOME=C:\Program Files\Apache\Tomcat 7"
    C:\Users\me> set "CATALINA_BASE=C:\Program Files\My Jenkins"
    C:\Users\me> cd "%CATALINA_BASE%"
    C:\Program Files\My Jenkins> mkdir "%CATALINA_BASE%\bin"
    C:\...Jenkins> mkdir "%CATALINA_BASE%\conf"
    C:\...Jenkins> mkdir "%CATALINA_BASE%\lib"
    C:\...Jenkins> mkdir "%CATALINA_BASE%\logs"
    C:\...Jenkins> mkdir "%CATALINA_BASE%\temp"
    C:\...Jenkins> mkdir "%CATALINA_BASE%\webapps"
    C:\...Jenkins> mkdir "%CATALINA_BASE%\work"
    C:\...Jenkins> copy "%CATALINA_HOME%\conf\*.*" "%CATALINA_BASE%\conf"
    C:\...Jenkins> copy "%CATALINA_HOME%\bin\*.exe" "%CATALINA_BASE%\bin"
    C:\...Jenkins> copy "%CATALINA_HOME%\bin\tomcat-juli.jar" "%CATALINA_BASE%\bin"
    C:\...Jenkins> cd bin
    C:\...Jenkins> ren tomcat7.exe jenkins.exe
    C:\...Jenkins> ren tomcat7w.exe jenkinsw.exe

The next command should be in a single line

    C:\...Jenkins> jenkins.exe //IS//Jenkins --DisplayName "Jenkins CI"
      --Description "Jenkins Continuous Integration Server on Tomcat"
      --Startup auto --JavaHome "%JAVA_HOME%" --StartMode jvm --StopMode jvm
      --StartClass org.apache.catalina.startup.Bootstrap
      --StopClass org.apache.catalina.startup.Bootstrap
      --StartParams start --StopParams stop
      --Classpath "%CATALINA_HOME%\bin\bootstrap.jar;%CATALINA_BASE%\bin\tomcat-juli.jar;"
      --StdOutput auto --StdError auto --LogLevel INFO
      --LogPath "%CATALINA_BASE%\logs"
      --JvmOptions -Xrs;-Dcatalina.home=%CATALINA_HOME%;-Dcatalina.base=%CATALINA_BASE%;

You should now have the Jenkins CI service in your "Services" window.

Deploy the Jenkins web application (WAR file) into %CATALINA_BASE%\webapps.

The steps I gave above are coming "from the the head" and are not tested, I might have missed a parameter or two. That is why there is the %CATALINA_BASE%\bin\jenkinsw.exe - start it and tweak the parameters until the server is working.

Cebence
  • 2,406
  • 2
  • 19
  • 20
  • You could write a `ServletContextListener` and run your BAT file - check my answer to [this question](http://stackoverflow.com/questions/9255003). – Cebence Mar 30 '12 at 19:45