How Tomcat Windows Service can ruin your weekend!
Context: We are currently using Tomcat 5.0.19. Since we use ANT script to do all the project setup, tomcat is installed from the zip distribution. Once we unzip the tomcat installation zip (that’s the installation process), we use a separate bat file to install tomcat as a windows service. Following is the bat file contents.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | @echo off if "%OS%" == "Windows_NT" setlocal rem --------------------------------------------------------------------------- rem NT Service Install/Uninstall script rem rem Options rem install Install the service using Tomcat5 as service name. rem Service is installed using default settings. rem remove Remove the service from the System. rem rem name (optional) If the second argument is present it is considered rem to be new service name rem CATALINA_HOME <Root_Dir>\Tomcat rem JAVA_HOME E:\jdk_1.3.0 rem JRE Type hotspot rem rem --------------------------------------------------------------------------- rem Guess CATALINA_HOME if not defined set CURRENT_DIR=%cd% set CATALINA_HOME=%3 if exist "%CATALINA_HOME%\bin\tomcat.exe" goto okHome :gotHome echo The tomcat.exe was not found... echo The CATALINA_HOME environment variable is not defined correctly. echo This environment variable is needed to run this program goto end :okHome set EXECUTABLE=%CATALINA_HOME%\bin\tomcat.exe rem Set default Service name set SERVICE_NAME=TomcatClearinghousePortal if "%1" == "" goto displayUsage if "%2" == "" goto setServiceName if "%3" == "" goto displayUsage if "%4" == "" goto displayUsage if "%5" == "" goto displayUsage set SERVICE_NAME=%2 :setServiceName if %1 == install goto doInstall if %1 == remove goto doRemove echo Unknown parameter "%1" :displayUsage echo echo Usage: service.bat install/remove [service_name] CATALINA_HOME JDK_HOME JRE_TYPE goto end :doRemove rem Remove the service "%EXECUTABLE%" //DS//%SERVICE_NAME% echo The service '%SERVICE_NAME%' has been removed goto end :doInstall rem Install the service %EXECUTABLE% //IS//%SERVICE_NAME% --DisplayName "%2" --Description "Indemand Clearinghouse Portal" %EXECUTABLE% //US//%SERVICE_NAME% --Install %EXECUTABLE% %EXECUTABLE% //US//%SERVICE_NAME% --ImagePath "%JAVA_HOME%\lib\tools.jar;%CATALINA_HOME%\bin\bootstrap.jar" %EXECUTABLE% //US//%SERVICE_NAME% --Java "%4\jre\bin\%5\jvm.dll" %EXECUTABLE% //US//%SERVICE_NAME% --StartupClass org.apache.catalina.startup.Bootstrap;main;start --ShutdownClass org.apache.catalina.startup.Bootstrap;main;stop --Startup auto rem Set extra parameters %EXECUTABLE% //US//%SERVICE_NAME% --JavaOptions -Dcatalina.home=""%CATALINA_HOME%""#-Djava.endorsed.dirs=""%CATALINA_HOME%\common\endorsed""#-Djava.io.tmpdir=""%CATALINA_HOME%\temp""#-Xms32m#-Xmx256m#-Xrs %EXECUTABLE% //US//%SERVICE_NAME% --StdOutputFile "%CATALINA_HOME%\logs\stdout.log" %EXECUTABLE% //US//%SERVICE_NAME% --StdErrorFile "%CATALINA_HOME%\logs\stderr.log" %EXECUTABLE% //US//%SERVICE_NAME% --WorkingPath "%CATALINA_HOME%\bin" echo The service '%SERVICE_NAME%' has been installed :end cd %CURRENT_DIR% |
This will install Tomcat as a windows service and works fine.
Symptoms: Recently we had some issues integrating Tomcat with some other java service. When we started tomcat from the command prompt, everything worked fine. But as soon as we started tomcat as windows service, we’re left counting stars. Then we started searching for any error messages in the Tomcat log. Surprisingly we found that, though tomcat was writing logs, there was nothing related to our web application. We tried to reinstall the tomcat service but still no log messages.
Call the doctor: At this stage we felt that we should call the doctor. Guess who? Chris Stevenson. He’s saved us before in similar windows services related issues. Chris suggested that there’s an issue with the script that we are using to install Tomcat as a windows service. This is not the standard script that comes with the tomcat installation.
1 2 3 4 5 6 7 8 9 10 | %EXECUTABLE% //IS//%SERVICE_NAME% --DisplayName "%2" --Description "Indemand Clearinghouse Portal" %EXECUTABLE% //US//%SERVICE_NAME% --Install %EXECUTABLE% %EXECUTABLE% //US//%SERVICE_NAME% --ImagePath "%JAVA_HOME%\lib\tools.jar;%CATALINA_HOME%\bin\bootstrap.jar" %EXECUTABLE% //US//%SERVICE_NAME% --Java "%4\jre\bin\%5\jvm.dll" %EXECUTABLE% //US//%SERVICE_NAME% --StartupClass org.apache.catalina.startup.Bootstrap;main;start --ShutdownClass org.apache.catalina.startup.Bootstrap;main;stop --Startup auto rem Set extra parameters %EXECUTABLE% //US//%SERVICE_NAME% --JavaOptions -Dcatalina.home=""%CATALINA_HOME%""#-Djava.endorsed.dirs=""%CATALINA_HOME%\common\endorsed""#-Djava.io.tmpdir=""%CATALINA_HOME%\temp""#-Xms32m#-Xmx256m#-Xrs %EXECUTABLE% //US//%SERVICE_NAME% --StdOutputFile "%CATALINA_HOME%\logs\stdout.log" %EXECUTABLE% //US//%SERVICE_NAME% --StdErrorFile "%CATALINA_HOME%\logs\stderr.log" %EXECUTABLE% //US//%SERVICE_NAME% --WorkingPath "%CATALINA_HOME%\bin" |
Can you smell some thing fishy?
We are installing the service once and then updating the service 8 times. After trying out different things for some time, we accidentally discovered the issue. Guess what?
1 | %EXECUTABLE% //US//%SERVICE_NAME% --JavaOptions -Dcatalina.home=""%CATALINA_HOME%""#-Djava.endorsed.dirs=""%CATALINA_HOME%\common\endorsed""#-Djava.io.tmpdir=""%CATALINA_HOME%\temp""#-Xms32m#-Xmx256m#-Xrs |
This line was the culprit. But as anyone would guess, it’s not the weird looking #. But it’s the tmpDir, that’s the culprit. When we checked, we didn’t have a temp dir under CATALINA_HOME. As soon as we created the temp folder, everything started working fine.
Root Cause: When tomcat is started as a service, it does not create any folders. If the temp folder is not present, it will behave strangely. Most of the things would work fine, but occasionally you might find some issues.
Lesson learnt: If tomcat is acting weird, check the logs, if the logs are not correct then there has to be some issue with the service installation script. Make sure that all the folders referred in the script are actually present.