在windows系統(tǒng)中tomcat服務(wù)的注冊和刪除,可以運(yùn)用%TOMCAT_HOME%\bin\service.bat 來完成。
注意:目前免安裝版的發(fā)布已經(jīng)區(qū)分平臺(tái)了(Tomcat6.0.24開始和 Tomcat7),需要下載windows平臺(tái)對應(yīng)的壓縮包,否則在bin目錄下沒有:service.bat、tomcatX.exe、tomcatXw.exe 等文件。
注冊服務(wù):
1 | >service.bat install |
刪除服務(wù):
1 | service.bat remove |
也可以通過bin\tomcat6.exe 來對服務(wù)的注冊和刪除:
tomcat6w.exe的命令可選參數(shù):
//ES// | Edit service configuration | This is the default operation. It is called if the no option is provided but the executable is renamed to servicenameW.exe |
---|---|---|
//MS// | Monitor service | Put the icon in the system try |
Command line arguments:
//TS// | Run the service as console application | This is the default operation. It is called if the no option is provided. The ServiceName is the name of the executable without exe suffix, meaning Tomcat6 |
---|---|---|
//RS// | Run the service | Called only from ServiceManager |
//SS// | Stop the service | |
//US// | Update service parameters | |
//IS// | Install service | |
//DS// | Delete service | Stops the service if running |
Command line parameters:
ParameterName | Default | Description |
---|---|---|
–Description | Service name description (maximum 1024 characters) | |
–DisplayName | ServiceName | Service display name |
–Install | procrun.exe //RS//ServiceName | Install image |
–Startup | manual | Service startup mode can be either auto or manual |
–DependsOn | List of services that this service depend on. Dependent services are separated using either # or ; characters | |
–Environment | List of environment variables that will be provided to the service in the form key=value. They are separated using either # or ; characters | |
–User | User account used for running executable. It is used only for StartMode java or exe and enables running applications as service under account without LogonAsService privilege. | |
–Password | Password for user account set by –User parameter | |
–JavaHome | JAVA_HOME | Set a different JAVA_HOME than defined by JAVA_HOME environment variable |
–Jvm | auto | Use either auto or specify the full path to the jvm.dll. You can use the environment variable expansion here. |
–JvmOptions | -Xrs | List of options in the form of -D or -X that will be passed to the JVM. The options are separated using either # or ; characters. |
–Classpath | Set the Java classpath | |
–JvmMs | Initial memory pool size in MB | |
–JvmMx | Maximum memory pool size in MB | |
–JvmSs | Thread stack size in KB | |
–StartImage | Executable that will be run. | |
–StartPath | Working path for the start image executable. | |
–StartClass | Class that will be used for startup. | |
–StartParams | List of parameters that will be passed to either StartImage or StartClass. Parameters are separated using either # or ; character. | |
–StartMethod | Main | Method name if differs then main |
–StartMode | executable | Can one of jvm java or exe |
–StopImage | Executable that will be run on Stop service signal. | |
–StopPath | Working path for the stop image executable. | |
–StopClass | Class that will be used on Stop service signal. | |
–StopParams | List of parameters that will be passed to either StopImage or StopClass. Parameters are separated using either # or ; character. | |
–StopMethod | Main | Method name if differs then main |
–StopMode | executable | Can one of jvm java or exe |
–StopTimeout | No Timeout | Defines the timeout in seconds that procrun waits for service to exit gracefully. |
–LogPath | working path | Defines the path for logging |
–LogPrefix | jakarta_service | Defines the service log filename |
–LogLevel | INFO | Defines the logging level and can be either error, info, warn or debug |
–StdOutput | Redirected stdout filename | |
–StdError | Redirected stderr filename |
注冊名稱為’Tomcat6′的服務(wù):
1 | Install the service named 'Tomcat6' |
2 | C:\> tomcat6 //IS//Tomcat6 --DisplayName= "Apache Tomcat 6" \ |
3 | C:\> --Install= "C:\Program Files\Tomcat\bin\tomcat6.exe" --Jvm=auto \ |
4 | C:\> --StartMode=jvm --StopMode=jvm \ |
5 | C:\> --StartClass=org.apache.catalina.startup.Bootstrap --StartParams=start \ |
6 | C:\> --StopClass=org.apache.catalina.startup.Bootstrap --StopParams=stop |
更新名稱為’Tomcat6′的服務(wù)信息
1 | C:\> tomcat6 //US//Tomcat6 --Description= "Apache Tomcat Server - |
2 | C:\> --Startup=auto --Classpath=%JAVA_HOME%\lib\tools.jar;%CATALINA_HOME%\bin\bootstrap.jar |
移除名稱叫’Tomcat6‘的服務(wù)
1 | C:\> tomcat6 //DS//Tomcat6 |
詳細(xì)見官網(wǎng):http://tomcat.apache.org/tomcat-6.0-doc/windows-service-howto.html
service.bat 的代碼 :
1 | @ echo off |
2 | if "%OS%" == "Windows_NT" setlocal |
3 | rem --------------------------------------------------------------------------- |
4 | rem NT Service Install/Uninstall script |
5 | rem |
6 | rem Options |
7 | rem install Install the service using Tomcat5 as service name. |
8 | rem Service is installed using default settings. |
9 | rem remove Remove the service from the System. |
10 | rem |
11 | rem name (optional) If the second argument is present it is considered |
12 | rem to be new service name |
13 | rem |
14 | rem $Id: service.bat 467182 2006-10-23 23:47:06Z markt $ |
15 | rem --------------------------------------------------------------------------- |
16 |
17 | rem Guess CATALINA_HOME if not defined |
18 | set CURRENT_DIR=% cd % |
19 | if not "%CATALINA_HOME%" == "" goto gotHome |
20 | set CATALINA_HOME=% cd % |
21 | if exist "%CATALINA_HOME%\bin\tomcat6.exe" goto okHome |
22 | rem CD to the upper dir |
23 | cd .. |
24 | set CATALINA_HOME=% cd % |
25 | :gotHome |
26 | if exist "%CATALINA_HOME%\bin\tomcat6.exe" goto okHome |
27 | echo The tomcat.exe was not found... |
28 | echo The CATALINA_HOME environment variable is not defined correctly. |
29 | echo This environment variable is needed to run this program |
30 | goto end |
31 | rem Make sure prerequisite environment variables are set |
32 | if not "%JAVA_HOME%" == "" goto okHome |
33 | echo The JAVA_HOME environment variable is not defined |
34 | echo This environment variable is needed to run this program |
35 | goto end |
36 | <img src= " |
37 | if not "%CATALINA_BASE%" == "" goto gotBase |
38 | set CATALINA_BASE=%CATALINA_HOME% |
39 | :gotBase |
40 |
41 | set EXECUTABLE=%CATALINA_HOME%\bin\tomcat6.exe |
42 |
43 | rem Set default Service name |
44 | set SERVICE_NAME=Tomcat6 |
45 | set PR_DISPLAYNAME=Apache Tomcat |
46 |
47 | if "%1" == "" goto displayUsage |
48 | if "%2" == "" goto setServiceName |
49 | set SERVICE_NAME=%2 |
50 | set PR_DISPLAYNAME=Apache Tomcat %2 |
51 | :setServiceName |
52 | if %1 == install goto doInstall |
53 | if %1 == remove goto doRemove |
54 | if %1 == uninstall goto doRemove |
55 | echo Unknown parameter "%1" |
56 | :displayUsage |
57 | echo . |
58 | echo Usage: service.bat install /remove [service_name] |
59 | goto end |
60 |
61 | :doRemove |
62 | rem Remove the service |
63 | "%EXECUTABLE%" //DS//%SERVICE_NAME% |
64 | echo The service '%SERVICE_NAME%' has been removed |
65 | goto end |
66 |
67 | :doInstall |
68 | rem Install the service |
69 | echo Installing the service '%SERVICE_NAME%' ... |
70 | echo Using CATALINA_HOME: %CATALINA_HOME% |
71 | echo Using CATALINA_BASE: %CATALINA_BASE% |
72 | echo Using JAVA_HOME: %JAVA_HOME% |
73 |
74 | rem Use the environment variables as an example |
75 | rem Each command line option is prefixed with PR_ |
76 |
77 | set PR_DESCRIPTION=Apache Tomcat Server - http://jakarta.apache.org/tomcat |
78 | set PR_INSTALL=%EXECUTABLE% |
79 | set PR_LOGPATH=%CATALINA_BASE%\logs |
80 | set PR_CLASSPATH=%CATALINA_HOME%\bin\bootstrap.jar |
81 | rem Set the server jvm from JAVA_HOME |
82 | set PR_JVM=%JAVA_HOME%\jre\bin\server\jvm.dll |
83 | if exist "%PR_JVM%" goto foundJvm |
84 | rem Set the client jvm from JAVA_HOME |
85 | set PR_JVM=%JAVA_HOME%\jre\bin\client\jvm.dll |
86 | if exist "%PR_JVM%" goto foundJvm |
87 | set PR_JVM=auto |
88 | :foundJvm |
89 | echo Using JVM: %PR_JVM% |
90 | "%EXECUTABLE%" //IS//%SERVICE_NAME% --StartClass org.apache.catalina.startup.Bootstrap --StopClass org.apache.catalina.startup.Bootstrap --StartParams start --StopParams stop |
91 | if not errorlevel 1 goto installed |
92 | echo Failed installing '%SERVICE_NAME%' service |
93 | goto end |
94 | :installed |
95 | rem Clear the environment variables. They are not needed any more . |
96 | set PR_DISPLAYNAME= |
97 | set PR_DESCRIPTION= |
98 | set PR_INSTALL= |
99 | set PR_LOGPATH= |
100 | set PR_CLASSPATH= |
101 | set PR_JVM= |
102 | rem Set extra parameters |
103 | "%EXECUTABLE%" //US//%SERVICE_NAME% --JvmOptions "-Dcatalina.base=%CATALINA_BASE%;-Dcatalina.home=%CATALINA_HOME%;-Djava.endorsed.dirs=%CATALINA_HOME%\endorsed" --StartMode jvm --StopMode jvm |
104 | rem More extra parameters |
105 | set PR_LOGPATH=%CATALINA_BASE%\logs |
106 | set PR_STDOUTPUT=auto |
107 | set PR_STDERROR=auto |
108 | "%EXECUTABLE%" //US//%SERVICE_NAME% ++JvmOptions "-Djava.io.tmpdir=%CATALINA_BASE%\temp" --JvmMs 128 --JvmMx 256 |
109 | echo The service '%SERVICE_NAME%' has been installed. |
110 |
111 | :end |
112 | cd %CURRENT_DIR% |