## Windows Service How-To

### Table of Contents

- Tomcat monitor application
  1. Command line directives
- Tomcat service application
  1. Command line directives
  2. Command line parameters
- Installing services
- Updating services
- Removing services
- Debugging services
- Multiple Instances

### Tomcat monitor application

Tomcat11w is a GUI application for monitoring and configuring Tomcat services.

#### Command line directives

Each command line directive is in the form of `//XX[//ServiceName]`

If the `//ServiceName` component is omitted, then the service name is assumed to be the name of the file less the w suffix. So the default service name is `Tomcat11`.

The available command line directives are:

//ES | Edit service configuration | This is the default operation. It is called if the no option is provided. Starts the GUI application which allows the service configuration to be modified, started and stopped.
//MS | Monitor service | Starts the GUI application and minimizes it to the system tray.
//MR | Monitor & run service | Starts the GUI application and minimizes it to the system tray. Start the service if it is not currently running.
//MQ | Monitor quit | Stop any running monitor for the service.

### Tomcat service application

Tomcat11 is a service application for running Tomcat 11 as a Windows service.

#### Command line directives

Each command line directive is in the form of `//XX[//ServiceName]`

The available command line directives are:

//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 Tomcat11
//RS | Run the service | Called only from ServiceManager
//ES | Start (execute) the service |
//SS | Stop the service |
//US | Update service parameters |
//IS | Install service |
//DS | Delete service | Stops the service if running
//PS | Print service | Prints the command to (re-)create the current configuration
//PP[//seconds] | Pause service | Default is 60 seconds
//VS | Version | Print version and exit
//? | Help | Print usage and exit

#### Command line parameters

Each command line parameter is prefixed with `--`. If the command line parameter is prefixed with `++`, and the parameter supports multiple values, then it's value will be appended to the existing option. In the table below, parameters that support multiple values are prefixed with `++`.

If the environment variable with the same name as command line parameter but prefixed with `PR_` exists it will take precedence. For example:

    set PR_CLASSPATH=xx.jar

is equivalent to providing

    --Classpath=xx.jar

as command line parameter.

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. If you need to use either the # or ; character within a value then the entire value must be enclosed inside single quotes.
--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
--ServiceUser |  | Specifies the name of the account under which the service should run. Use an account name in the form `DomainName\UserName`. The service process will be logged on as this user. if the account belongs to the built-in domain, you can specify `.\UserName`. Note that the Service Control Manager does not accept localised forms of the standard names so to use them you need to specify `NT Authority\LocalService`, `NT Authority\NetworkService` or `LocalSystem` as appropriate.
--ServicePassword |  | Password for user account set by --ServiceUser parameter
--LibraryPath |  | Directory added to the search path used to locate the DLLs for the JVM. This directory is added both in front of the `PATH` environment variable and as a parameter to the `SetDLLDirectory` function.
--JavaHome | JAVA_HOME | Set a different JAVA_HOME than defined by JAVA_HOME environment variable
--Jvm | auto | Use either auto (i.e. find the JVM from the Windows registry) 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. If you need to embed either # or ; characters, put them inside single quotes. (Not used in exe mode.)
++JvmOptions9 |  | List of options in the form of -D or -X that will be passed to the JVM when running on Java 9 or later. The options are separated using either # or ; characters. If you need to embed either # or ; characters, put them inside single quotes. (Not used in exe mode.)
--Classpath |  | Set the Java classpath. (Not used in exe mode.)
--JvmMs |  | Initial memory pool size in MB. (Not used in exe mode.)
--JvmMx |  | Maximum memory pool size in MB. (Not used in exe mode.)
--JvmSs |  | Thread stack size in KB. (Not used in exe mode.)
--StartMode |  | One of jvm, Java or exe. The modes are: jvm - start Java in-process. Depends on jvm.dll, see --Jvm. Java - same as exe, but automatically uses the default Java executable, i.e. %JAVA_HOME%\bin\java.exe. Make sure JAVA_HOME is set correctly, or use --JavaHome to provide the correct location. If neither is set, procrun will try to find the default JDK (not JRE) from the Windows registry. exe - run the image as a separate process
--StartImage |  | Executable that will be run. Only applies to exe mode.
--StartPath |  | Working path for the start image executable.
--StartClass | Main | Class that contains the startup method. Applies to the jvm and Java modes. (Not used in exe mode.)
--StartMethod | main | Method name if differs then main
++StartParams |  | List of parameters that will be passed to either StartImage or StartClass. Parameters are separated using either # or ; character.
--StopMode |  | One of jvm, Java or exe. See --StartMode for further details.
--StopImage |  | Executable that will be run on Stop service signal. Only applies to exe mode.
--StopPath |  | Working path for the stop image executable. Does not apply to jvm mode.
--StopClass | Main | Class that will be used on Stop service signal. Applies to the jvm and Java modes.
--StopMethod | main | Method name if differs then main
--StopParams |  | List of parameters that will be passed to either StopImage or StopClass. Parameters are separated using either # or ; character.
++StopTimeout | No Timeout | Defines the timeout in seconds that procrun waits for service to exit gracefully.
--LogPath | %SystemRoot%\System32\LogFiles\Apache | Defines the path for logging. Creates the directory if necessary.
--LogPrefix | commons-daemon | Defines the service log filename prefix. The log file is created in the LogPath directory with `.YEAR-MONTH-DAY.log` suffix
--LogLevel | Info | Defines the logging level and can be either Error, Info, Warn or Debug. (Case insensitive).
--LogJniMessages | 0 | Set this non-zero (e.g. 1) to capture JVM jni debug messages in the procrun log file. Is not needed if stdout/stderr redirection is being used. Only applies to jvm mode.
--StdOutput |  | Redirected stdout filename. If named auto then file is created inside LogPath with the name service-stdout.YEAR-MONTH-DAY.log.
--StdError |  | Redirected stderr filename. If named auto then file is created inside LogPath with the name service-stderr.YEAR-MONTH-DAY.log.
--PidFile |  | Defines the file name for storing the running process id. Actual file is created in the LogPath directory

### Installing services

The safest way to manually install the service is to use the provided service.bat script. Administrator privileges are required to run this script. If necessary, you can use the `/user` switch to specify a user to use for the installation of the service.

NOTE: If User Account Control (UAC) is enabled you will be asked for additional privileges when 'Tomcat11.exe' is launched by the script.
If you want to pass additional options to service installer as `PR_*` environment variables, you have to either configure them globally in OS, or launch the program that sets them with elevated privileges (e.g. right-click on cmd.exe and select "Run as administrator"; on Windows 8 (or later) or Windows Server 2012 (or later), you can open an elevated command prompt for the current directory from the Explorer by clicking on the "File" menu bar). See issue 56143 for details.

    Install the service named 'Tomcat11'
    C:\> service.bat install

There is a 2nd optional parameter that lets you specify the name of the service, as displayed in Windows services.

    Install the service named 'MyService'
    C:\> service.bat install MyService

When installing the service with a non-default name, tomcat11.exe and tomcat11w.exe may be renamed to match the chosen service name. To do this, use the `--rename` option.

    Install the service named 'MyService' with renaming
    C:\> service.bat install MyService --rename

If using tomcat11.exe, you need to use the //IS parameter.

    Install the service named 'Tomcat11'
    C:\> tomcat11 //IS//Tomcat11 --DisplayName="Apache Tomcat 11" ^
         --Install="C:\Program Files\Tomcat\bin\tomcat11.exe" --Jvm=auto ^
         --StartMode=jvm --StopMode=jvm ^
         --StartClass=org.apache.catalina.startup.Bootstrap --StartParams=start ^
         --StopClass=org.apache.catalina.startup.Bootstrap --StopParams=stop

### Updating services

To update the service parameters, you need to use the //US parameter.

    Update the service named 'Tomcat11'
    C:\> tomcat11 //US//Tomcat11 --Description="Apache Tomcat Server - https://tomcat.apache.org/ " ^
         --Startup=auto --Classpath=%JAVA_HOME%\lib\tools.jar;%CATALINA_HOME%\bin\bootstrap.jar

If you gave the service an optional name, you need to specify it like this:

    Update the service named 'MyService'
    C:\> tomcat11 //US//MyService --Description="Apache Tomcat Server - https://tomcat.apache.org/ " ^
         --Startup=auto --Classpath=%JAVA_HOME%\lib\tools.jar;%CATALINA_HOME%\bin\bootstrap.jar

### Removing services

To remove the service, you need to use the //DS parameter.
If the service is running it will be stopped and then deleted.

    Remove the service named 'Tomcat11'
    C:\> tomcat11 //DS//Tomcat11

If you gave the service an optional name, you need to specify it like this:

    Remove the service named 'MyService'
    C:\> tomcat11 //DS//MyService

### Debugging services

To run the service in console mode, you need to use the //TS parameter. The service shutdown can be initiated by pressing CTRL+C or CTRL+BREAK. If you rename the tomcat11.exe to testservice.exe then you can just execute the testservice.exe and this command mode will be executed by default.

    Run the service named 'Tomcat11' in console mode
    C:\> tomcat11 //TS//Tomcat11 [additional arguments]
    Or simply execute:
    C:\> tomcat11

### Multiple Instances

Tomcat supports installation of multiple instances. You can have a single installation of Tomcat with multiple instances running on different IP/port combinations, or multiple Tomcat versions, each running one or more instances on different IP/ports.

Each instance folder will need the following structure:

- conf
- logs
- temp
- webapps
- work

At a minimum, conf should contain a copy of the following files from CATALINA_HOME\conf\. Any files not copied and edited, will be picked up by default from CATALINA_HOME\conf, i.e. CATALINA_BASE\conf files override defaults from CATALINA_HOME\conf.

- server.xml
- web.xml

You must edit CATALINA_BASE\conf\server.xml to specify a unique IP/port for the instance to listen on. Find the line that contains `<Connector port="8080" ...` and add an address attribute and/or update the port number so as to specify a unique IP/port combination.

To install an instance, first set the CATALINA_HOME environment variable to the name of the Tomcat installation directory. Then create a second environment variable CATALINA_BASE and point this to the instance folder. Then run "service.bat install" command specifying a service name.

    set CATALINA_HOME=c:\tomcat_11
    set CATALINA_BASE=c:\tomcat_11\instances\instance1
    service.bat install instance1

To modify the service settings, you can run tomcat11w //ES//instance1.

For additional instances, create additional instance folder, update the CATALINA_BASE environment variable, and run the "service.bat install" again.

    set CATALINA_BASE=c:\tomcat_11\instances\instance2
    service.bat install instance2