Tomcat发布为Windows服务

来源:互联网 发布:淘宝客自动发送微信群 编辑:程序博客网 时间:2024/05/18 07:38
在将tomcat部署到服务器的时候,如果依然像在开发中直接打开startup.bat来运行的话,首先是有一个黑色的框显示在桌面上,非常不好看,而且很容易就给关闭掉了,也不安全。另外如果服务器重启了,我们还需要手动的来启动tomcat,更是给部署带来了问题。同样的,可能有朋友试过把startup.bat放到Windows的启动目录下,那么必须要进行用户登录,启动目录下的程序才会启动,因此也不是很好的解决方案。那么到底有没有方法来进行处理呢,当然有,而且tomcat已经为我们想到了。就是将tomcat发布为Windows的服务,然后设置服务为自动启动就可以了。

发布为tomcat服务

在bin目录下有一个service.bat,使用  service.bat install 就可以发布为服务了

删除tomcat服务
service.bat uninstall

我们在发布为tomcat服务的时候,服务默认是手动启动,而且在发布完毕之后,并不是启动的。那么我们需要通过Windows的服务管理来进行启动服务和设置服务自动启动。



找到一个  Apache tomcat开头的服务,那个就是你刚才所发布的服务。设置为自动即可。


服务改名
如果我们不想使用tomcat给我们指定的默认的服务名,想起一个我们自己的名字可不可以呢,当然也是可以的,这时候可以修改service.bat来实现了。源码如下,修改第76行对应的内容即可

@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.exe" goto okHomerem CD to the upper dircd ..set "CATALINA_HOME=%cd%":gotHomeif exist "%CATALINA_HOME%\bin\tomcat7.exe" goto okHomeecho The tomcat7.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.exe"rem Set default Service name 这里就是设置服务名称和服务的描述,修改这里即可set SERVICE_NAME=Tomcat7set DISPLAYNAME=Apache Tomcat 7.0 %SERVICE_NAME%if "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.78 Server - 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 128 ^    --JvmMx 256if not errorlevel 1 goto installedecho Failed installing '%SERVICE_NAME%' servicegoto end:installedecho The service '%SERVICE_NAME%' has been installed.:endcd "%CURRENT_DIR%"




原创粉丝点击