在windows下多个tomcat实例注册成windows服务

来源:互联网 发布:如何编写数据库引擎 编辑:程序博客网 时间:2024/06/05 16:05

以下内容仅作为个人备忘和参考!以下用绿色版tomcat7.0.62来说明。

第一个tomcat服务无所谓,直接用service.bat install 注册,一切都默认;

第二个和以后的需要修改service.bat文件(%CATALINA_HOME%/bin目录下),步骤如下:


1.为保险起见,在service.bat的最上方重置两个变量,如果系统的环境变量没配的话可以忽略这一步

set "CATALINA_HOME=D:\apache-tomcat-7.0.62-jenkins-2"
set "CATALINA_BASE=%CATALINA_HOME%"


2.继续修改service.bat,修改服务名称
set SERVICE_NAME=Tomcat7-jenkins2 
set DISPLAYNAME=Apache Tomcat 7.0 %SERVICE_NAME%  for jenkins
DISPLAYNAME是用来在windows服务管理器里显示的服务名称
SERVICE_NAME是注册到windows的真是名称


3.继续修改service.bat,把用到tomcat7.exe的地方重新命名一下我这里是“tomcat7-jenkins-2.exe”;


4.到tomcat根目录修改tomcat7w.exe的名称为%SERVICE_NAME%的变量值,这个步骤直接影响可视化配置界面是否能打开;

   接着修改tomcat7.exe的名称为“tomcat7-jenkins-2.exe”,这里要和第三步里重新命名的名称一致,这个是windows进程表里显示的名称,如果不改的话多个tomcat都是一样的,不好区分。


5.最后到dos窗口执行 service.bat install (先cd到service.bat目录或者直接输入全路径)。


贴一下service.bat的内容:

set "CATALINA_HOME=D:\apache-tomcat-7.0.62-jenkins-2"set "CATALINA_BASE=%CATALINA_HOME%"@echo offrem Licensed to the Apache Software Foundation (ASF) under one or morerem contributor license agreements.  See the NOTICE file distributed withrem this work for additional information regarding copyright ownership.rem The ASF licenses this file to You under the Apache License, Version 2.0rem (the "License"); you may not use this file except in compliance withrem the License.  You may obtain a copy of the License atremrem     http://www.apache.org/licenses/LICENSE-2.0remrem Unless required by applicable law or agreed to in writing, softwarerem distributed under the License is distributed on an "AS IS" BASIS,rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.rem See the License for the specific language governing permissions andrem limitations under the License.rem ---------------------------------------------------------------------------rem NT Service Install/Uninstall scriptremrem Optionsrem install                Install the service using Tomcat7 as service name.rem                        Service is installed using default settings.rem remove                 Remove the service from the System.remrem name        (optional) If the second argument is present it is consideredrem                        to be new service namerem ---------------------------------------------------------------------------setlocalset "SELF=%~dp0%service.bat"rem Guess CATALINA_HOME if not definedset "CURRENT_DIR=%cd%"if not "%CATALINA_HOME%" == "" goto gotHomeset "CATALINA_HOME=%cd%"if exist "%CATALINA_HOME%\bin\tomcat7-jenkins-2.exe" goto okHomerem CD to the upper dircd ..set "CATALINA_HOME=%cd%":gotHomeif exist "%CATALINA_HOME%\bin\tomcat7-jenkins-2.exe" goto okHomeecho The tomcat7-jenkins-2.exe was not found...echo The CATALINA_HOME environment variable is not defined correctly.echo This environment variable is needed to run this programgoto end:okHomerem Make sure prerequisite environment variables are setif not "%JAVA_HOME%" == "" goto gotJdkHomeif not "%JRE_HOME%" == "" goto gotJreHomeecho Neither the JAVA_HOME nor the JRE_HOME environment variable is definedecho Service will try to guess them from the registry.goto okJavaHome:gotJreHomeif not exist "%JRE_HOME%\bin\java.exe" goto noJavaHomeif not exist "%JRE_HOME%\bin\javaw.exe" goto noJavaHomegoto okJavaHome:gotJdkHomeif not exist "%JAVA_HOME%\jre\bin\java.exe" goto noJavaHomeif not exist "%JAVA_HOME%\jre\bin\javaw.exe" goto noJavaHomeif not exist "%JAVA_HOME%\bin\javac.exe" goto noJavaHomeif not "%JRE_HOME%" == "" goto okJavaHomeset "JRE_HOME=%JAVA_HOME%\jre"goto okJavaHome:noJavaHomeecho The JAVA_HOME environment variable is not defined correctlyecho This environment variable is needed to run this programecho NB: JAVA_HOME should point to a JDK not a JREgoto end:okJavaHomeif not "%CATALINA_BASE%" == "" goto gotBaseset "CATALINA_BASE=%CATALINA_HOME%":gotBaseset "EXECUTABLE=%CATALINA_HOME%\bin\tomcat7-jenkins-2.exe"rem Set default Service nameset SERVICE_NAME=Tomcat7-jenkins2 set DISPLAYNAME=Apache Tomcat 7.0 %SERVICE_NAME%  for jenkinsif "x%1x" == "xx" goto displayUsageset SERVICE_CMD=%1shiftif "x%1x" == "xx" goto checkServiceCmd:checkUserif "x%1x" == "x/userx" goto runAsUserif "x%1x" == "x--userx" goto runAsUserset SERVICE_NAME=%1set DISPLAYNAME=Apache Tomcat 7.0 %1shiftif "x%1x" == "xx" goto checkServiceCmdgoto checkUser:runAsUsershiftif "x%1x" == "xx" goto displayUsageset SERVICE_USER=%1shiftrunas /env /savecred /user:%SERVICE_USER% "%COMSPEC% /K \"%SELF%\" %SERVICE_CMD% %SERVICE_NAME%"goto end:checkServiceCmdif /i %SERVICE_CMD% == install goto doInstallif /i %SERVICE_CMD% == remove goto doRemoveif /i %SERVICE_CMD% == uninstall goto doRemoveecho Unknown parameter "%SERVICE_CMD%":displayUsageecho.echo Usage: service.bat install/remove [service_name] [/user username]goto end:doRemoverem Remove the serviceecho Removing the service '%SERVICE_NAME%' ...echo Using CATALINA_BASE:    "%CATALINA_BASE%""%EXECUTABLE%" //DS//%SERVICE_NAME% ^    --LogPath "%CATALINA_BASE%\logs"if not errorlevel 1 goto removedecho Failed removing '%SERVICE_NAME%' servicegoto end:removedecho The service '%SERVICE_NAME%' has been removedgoto end:doInstallrem Install the serviceecho Installing the service '%SERVICE_NAME%' ...echo Using CATALINA_HOME:    "%CATALINA_HOME%"echo Using CATALINA_BASE:    "%CATALINA_BASE%"echo Using JAVA_HOME:        "%JAVA_HOME%"echo Using JRE_HOME:         "%JRE_HOME%"rem Try to use the server jvmset "JVM=%JRE_HOME%\bin\server\jvm.dll"if exist "%JVM%" goto foundJvmrem Try to use the client jvmset "JVM=%JRE_HOME%\bin\client\jvm.dll"if exist "%JVM%" goto foundJvmecho Warning: Neither 'server' nor 'client' jvm.dll was found at JRE_HOME.set JVM=auto:foundJvmecho Using JVM:              "%JVM%"set "CLASSPATH=%CATALINA_HOME%\bin\bootstrap.jar;%CATALINA_BASE%\bin\tomcat-juli.jar"if not "%CATALINA_HOME%" == "%CATALINA_BASE%" set "CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\bin\tomcat-juli.jar""%EXECUTABLE%" //IS//%SERVICE_NAME% ^    --Description "Apache Tomcat 7.0.62 Server - jenkins2 - http://tomcat.apache.org/" ^    --DisplayName "%DISPLAYNAME%" ^    --Install "%EXECUTABLE%" ^    --LogPath "%CATALINA_BASE%\logs" ^    --StdOutput auto ^    --StdError auto ^    --Classpath "%CLASSPATH%" ^    --Jvm "%JVM%" ^    --StartMode jvm ^    --StopMode jvm ^    --StartPath "%CATALINA_HOME%" ^    --StopPath "%CATALINA_HOME%" ^    --StartClass org.apache.catalina.startup.Bootstrap ^    --StopClass org.apache.catalina.startup.Bootstrap ^    --StartParams start ^    --StopParams stop ^    --JvmOptions "-Dcatalina.home=%CATALINA_HOME%;-Dcatalina.base=%CATALINA_BASE%;-Djava.endorsed.dirs=%CATALINA_HOME%\endorsed;-Djava.io.tmpdir=%CATALINA_BASE%\temp;-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager;-Djava.util.logging.config.file=%CATALINA_BASE%\conf\logging.properties" ^    --JvmMs 256 ^    --JvmMx 1024if not errorlevel 1 goto installedecho Failed installing '%SERVICE_NAME%' servicegoto end:installedecho The service '%SERVICE_NAME%' has been installed.:endcd "%CURRENT_DIR%"


0 0
原创粉丝点击