service.bat 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. @echo off
  2. rem Licensed to the Apache Software Foundation (ASF) under one or more
  3. rem contributor license agreements. See the NOTICE file distributed with
  4. rem this work for additional information regarding copyright ownership.
  5. rem The ASF licenses this file to You under the Apache License, Version 2.0
  6. rem (the "License"); you may not use this file except in compliance with
  7. rem the License. You may obtain a copy of the License at
  8. rem
  9. rem http://www.apache.org/licenses/LICENSE-2.0
  10. rem
  11. rem Unless required by applicable law or agreed to in writing, software
  12. rem distributed under the License is distributed on an "AS IS" BASIS,
  13. rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. rem See the License for the specific language governing permissions and
  15. rem limitations under the License.
  16. rem ---------------------------------------------------------------------------
  17. rem NT Service Install/Uninstall script
  18. rem
  19. rem Usage: service.bat install/remove [service_name [--rename]] [--user username]
  20. rem
  21. rem Options
  22. rem install Install the service using default settings.
  23. rem remove Remove the service from the system.
  24. rem
  25. rem service_name (optional) The name to use for the service. If not specified,
  26. rem Tomcat8 is used as the service name.
  27. rem
  28. rem --rename (optional) Rename tomcat8.exe and tomcat8w.exe to match
  29. rem the non-default service name.
  30. rem
  31. rem username (optional) The name of the OS user to use to install/remove
  32. rem the service (not the name of the OS user the
  33. rem service will run as). If not specified, the current
  34. rem user is used.
  35. rem ---------------------------------------------------------------------------
  36. setlocal
  37. set "SELF=%~dp0%service.bat"
  38. set DEFAULT_SERVICE_NAME=Tomcat8
  39. set SERVICE_NAME=%DEFAULT_SERVICE_NAME%
  40. set "CURRENT_DIR=%cd%"
  41. rem Parse the arguments
  42. if "x%1x" == "xx" goto displayUsage
  43. set SERVICE_CMD=%1
  44. shift
  45. if "x%1x" == "xx" goto checkEnv
  46. :checkUser
  47. if "x%1x" == "x/userx" goto runAsUser
  48. if "x%1x" == "x--userx" goto runAsUser
  49. set SERVICE_NAME=%1
  50. shift
  51. if "x%1x" == "xx" goto checkEnv
  52. if "x%1x" == "x--renamex" (
  53. set RENAME=%1
  54. shift
  55. )
  56. if "x%1x" == "xx" goto checkEnv
  57. goto checkUser
  58. :runAsUser
  59. shift
  60. if "x%1x" == "xx" goto displayUsage
  61. set SERVICE_USER=%1
  62. shift
  63. runas /env /savecred /user:%SERVICE_USER% "%COMSPEC% /K \"%SELF%\" %SERVICE_CMD% %SERVICE_NAME%"
  64. exit /b 0
  65. rem Check the environment
  66. :checkEnv
  67. rem Guess CATALINA_HOME if not defined
  68. if not "%CATALINA_HOME%" == "" goto gotHome
  69. set "CATALINA_HOME=%cd%"
  70. if exist "%CATALINA_HOME%\bin\%DEFAULT_SERVICE_NAME%.exe" goto gotHome
  71. if exist "%CATALINA_HOME%\bin\%SERVICE_NAME%.exe" goto gotHome
  72. rem CD to the upper dir
  73. cd ..
  74. set "CATALINA_HOME=%cd%"
  75. :gotHome
  76. if exist "%CATALINA_HOME%\bin\%DEFAULT_SERVICE_NAME%.exe" (
  77. set "EXECUTABLE=%CATALINA_HOME%\bin\%DEFAULT_SERVICE_NAME%.exe"
  78. goto okHome
  79. )
  80. if exist "%CATALINA_HOME%\bin\%SERVICE_NAME%.exe" (
  81. set "EXECUTABLE=%CATALINA_HOME%\bin\%SERVICE_NAME%.exe"
  82. goto okHome
  83. )
  84. if "%DEFAULT_SERVICE_NAME%"== "%SERVICE_NAME%" (
  85. echo The file %DEFAULT_SERVICE_NAME%.exe was not found...
  86. ) else (
  87. echo Neither the %DEFAULT_SERVICE_NAME%.exe file nor the %SERVICE_NAME%.exe file was found...
  88. )
  89. echo Either the CATALINA_HOME environment variable is not defined correctly or
  90. echo the incorrect service name has been used.
  91. echo Both the CATALINA_HOME environment variable and the correct service name
  92. echo are required to run this program.
  93. exit /b 1
  94. :okHome
  95. cd "%CURRENT_DIR%"
  96. rem Make sure prerequisite environment variables are set
  97. if not "%JAVA_HOME%" == "" goto gotJdkHome
  98. if not "%JRE_HOME%" == "" goto gotJreHome
  99. echo Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
  100. echo Service will try to guess them from the registry.
  101. goto okJavaHome
  102. :gotJreHome
  103. if not exist "%JRE_HOME%\bin\java.exe" goto noJavaHome
  104. goto okJavaHome
  105. :gotJdkHome
  106. if not exist "%JAVA_HOME%\bin\javac.exe" goto noJavaHome
  107. rem Java 9 has a different directory structure
  108. if exist "%JAVA_HOME%\jre\bin\java.exe" goto preJava9Layout
  109. if not exist "%JAVA_HOME%\bin\java.exe" goto noJavaHome
  110. if not "%JRE_HOME%" == "" goto okJavaHome
  111. set "JRE_HOME=%JAVA_HOME%"
  112. goto okJavaHome
  113. :preJava9Layout
  114. if not "%JRE_HOME%" == "" goto okJavaHome
  115. set "JRE_HOME=%JAVA_HOME%\jre"
  116. goto okJavaHome
  117. :noJavaHome
  118. echo The JAVA_HOME environment variable is not defined correctly
  119. echo This environment variable is needed to run this program
  120. echo NB: JAVA_HOME should point to a JDK not a JRE
  121. exit /b 1
  122. :okJavaHome
  123. if not "%CATALINA_BASE%" == "" goto gotBase
  124. set "CATALINA_BASE=%CATALINA_HOME%"
  125. :gotBase
  126. rem Java 9 no longer supports the java.endorsed.dirs
  127. rem system property. Only try to use it if
  128. rem JAVA_ENDORSED_DIRS was explicitly set
  129. rem or CATALINA_HOME/endorsed exists.
  130. set ENDORSED_PROP=ignore.endorsed.dirs
  131. if "%JAVA_ENDORSED_DIRS%" == "" goto noEndorsedVar
  132. set ENDORSED_PROP=java.endorsed.dirs
  133. goto doneEndorsed
  134. :noEndorsedVar
  135. if not exist "%CATALINA_HOME%\endorsed" goto doneEndorsed
  136. set ENDORSED_PROP=java.endorsed.dirs
  137. :doneEndorsed
  138. rem Process the requested command
  139. if /i %SERVICE_CMD% == install goto doInstall
  140. if /i %SERVICE_CMD% == remove goto doRemove
  141. if /i %SERVICE_CMD% == uninstall goto doRemove
  142. echo Unknown parameter "%SERVICE_CMD%"
  143. :displayUsage
  144. echo.
  145. echo Usage: service.bat install/remove [service_name [--rename]] [--user username]
  146. exit /b 1
  147. :doRemove
  148. rem Remove the service
  149. echo Removing the service '%SERVICE_NAME%' ...
  150. echo Using CATALINA_BASE: "%CATALINA_BASE%"
  151. "%EXECUTABLE%" //DS//%SERVICE_NAME% ^
  152. --LogPath "%CATALINA_BASE%\logs"
  153. if not errorlevel 1 goto removed
  154. echo Failed removing '%SERVICE_NAME%' service
  155. exit /b 1
  156. :removed
  157. echo The service '%SERVICE_NAME%' has been removed
  158. if exist "%CATALINA_HOME%\bin\%SERVICE_NAME%.exe" (
  159. rename "%SERVICE_NAME%.exe" "%DEFAULT_SERVICE_NAME%.exe"
  160. rename "%SERVICE_NAME%w.exe" "%DEFAULT_SERVICE_NAME%w.exe"
  161. )
  162. exit /b 0
  163. :doInstall
  164. rem Install the service
  165. echo Installing the service '%SERVICE_NAME%' ...
  166. echo Using CATALINA_HOME: "%CATALINA_HOME%"
  167. echo Using CATALINA_BASE: "%CATALINA_BASE%"
  168. echo Using JAVA_HOME: "%JAVA_HOME%"
  169. echo Using JRE_HOME: "%JRE_HOME%"
  170. rem Try to use the server jvm
  171. set "JVM=%JRE_HOME%\bin\server\jvm.dll"
  172. if exist "%JVM%" goto foundJvm
  173. rem Try to use the client jvm
  174. set "JVM=%JRE_HOME%\bin\client\jvm.dll"
  175. if exist "%JVM%" goto foundJvm
  176. echo Warning: Neither 'server' nor 'client' jvm.dll was found at JRE_HOME.
  177. set JVM=auto
  178. :foundJvm
  179. echo Using JVM: "%JVM%"
  180. set "CLASSPATH=%CATALINA_HOME%\bin\bootstrap.jar;%CATALINA_BASE%\bin\tomcat-juli.jar"
  181. if not "%CATALINA_HOME%" == "%CATALINA_BASE%" set "CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\bin\tomcat-juli.jar"
  182. if "%SERVICE_STARTUP_MODE%" == "" set SERVICE_STARTUP_MODE=manual
  183. if "%JvmMs%" == "" set JvmMs=128
  184. if "%JvmMx%" == "" set JvmMx=256
  185. if exist "%CATALINA_HOME%\bin\%DEFAULT_SERVICE_NAME%.exe" (
  186. if "x%RENAME%x" == "x--renamex" (
  187. rename "%DEFAULT_SERVICE_NAME%.exe" "%SERVICE_NAME%.exe"
  188. rename "%DEFAULT_SERVICE_NAME%w.exe" "%SERVICE_NAME%w.exe"
  189. set "EXECUTABLE=%CATALINA_HOME%\bin\%SERVICE_NAME%.exe"
  190. )
  191. )
  192. "%EXECUTABLE%" //IS//%SERVICE_NAME% ^
  193. --Description "Apache Tomcat 8.5.66 Server - https://tomcat.apache.org/" ^
  194. --DisplayName "Apache Tomcat 8.5 %SERVICE_NAME%" ^
  195. --Install "%EXECUTABLE%" ^
  196. --LogPath "%CATALINA_BASE%\logs" ^
  197. --StdOutput auto ^
  198. --StdError auto ^
  199. --Classpath "%CLASSPATH%" ^
  200. --Jvm "%JVM%" ^
  201. --StartMode jvm ^
  202. --StopMode jvm ^
  203. --StartPath "%CATALINA_HOME%" ^
  204. --StopPath "%CATALINA_HOME%" ^
  205. --StartClass org.apache.catalina.startup.Bootstrap ^
  206. --StopClass org.apache.catalina.startup.Bootstrap ^
  207. --StartParams start ^
  208. --StopParams stop ^
  209. --JvmOptions "-Dcatalina.home=%CATALINA_HOME%;-Dcatalina.base=%CATALINA_BASE%;-D%ENDORSED_PROP%=%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;%JvmArgs%" ^
  210. --JvmOptions9 "--add-opens=java.base/java.lang=ALL-UNNAMED#--add-opens=java.base/java.io=ALL-UNNAMED#--add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED" ^
  211. --Startup "%SERVICE_STARTUP_MODE%" ^
  212. --JvmMs "%JvmMs%" ^
  213. --JvmMx "%JvmMx%"
  214. if not errorlevel 1 goto installed
  215. echo Failed installing '%SERVICE_NAME%' service
  216. exit /b 1
  217. :installed
  218. echo The service '%SERVICE_NAME%' has been installed.
  219. exit /b 0