Chapter 25. Run Red Hat build of Keycloak as a Windows Service
Install and run Red Hat build of Keycloak as a Windows service using Apache Commons Daemon.
This guide explains how to install and run Red Hat build of Keycloak as a Windows service using Apache Commons Daemon. The service runs kc.bat in "exe" mode so behavior matches running kc.bat start manually. The service runs in "exe" mode, where Procrun executes kc.bat start as an external process. Environment variables, such as KC_*, along with conf/keycloak.conf, are respected. The kc.bat script handles augmentation and build logic, ensuring the service behaves exactly like a manual start.
25.1. Apache Commons Daemon Setup Copy linkLink copied to clipboard!
To run Red Hat build of Keycloak as a Windows service, you need the Apache Commons Daemon Procrun binary (prunsrv.exe). Download it for your platform: https://downloads.apache.org/commons/daemon/binaries/windows/
Then place prunsrv.exe into the Keycloak bin folder.
copy "path\to\prunsrv.exe" "%KEYCLOAK_HOME%\bin\prunsrv.exe"
Use the amd64 binary for 64-bit and x86 for 32-bit systems.
25.2. Optional: Pre-build Copy linkLink copied to clipboard!
Pre-building is optional. If you do not pre-build, Keycloak will build automatically on first start, which takes longer.
bin/kc.bat build --db=postgres
25.3. Installing the Service Copy linkLink copied to clipboard!
Keycloak includes a tools windows-service subcommand to simplify service installation and uninstallation.
bin\kc.bat tools windows-service install --help
bin\kc.bat tools windows-service uninstall --help
25.3.1. Examples Copy linkLink copied to clipboard!
Install a basic service (runs as Local System by default):
bin\kc.bat tools windows-service install --name keycloak
Manual startup and longer stop timeout:
bin\kc.bat tools windows-service install --startup=manual --stop-timeout=60
Delayed auto-start:
bin\kc.bat tools windows-service install --startup=delayed
Custom display name:
bin\kc.bat tools windows-service install --name=my-keycloak --display-name="My Keycloak Server"
Use --depends-on to ensure required Windows services start before Keycloak (for example, a local database). By default Apache Commons Daemon may add Tcpip and Afd network dependencies.
bin\kc.bat tools windows-service install --depends-on="postgresql-x64-15;Tcpip;Afd"
The default is to run the service as the Local System account - --service-user and --service-password can be omitted (recommended). To run as a specific user, the account must have the "Log on as a service" right.
bin\kc.bat tools windows-service install --service-user="DOMAIN\Username" --service-password="password"
You can supply the service password securely via an environment variable, which is recommended:
set KC_SERVICE_PASSWORD=s3cret
bin\kc.bat tools windows-service install --service-user="DOMAIN\Username"
Start the service:
net start keycloak
Stop the service:
net stop keycloak
Uninstall the service:
bin\kc.bat tools windows-service uninstall --name keycloak
Check status using the Windows Services console (services.msc).
25.4. Logging Copy linkLink copied to clipboard!
When Red Hat build of Keycloak runs as a service, it is recommended to enable file logging - see Configuring logging.
The service wrapper logs (e.g. commons-daemon.YYYY-MM-DD.log) respects the log-path option value during service creation.
25.5. Configuration Changes Copy linkLink copied to clipboard!
To change runtime configuration:
-
Stop the service:
net stop keycloak. -
Update environment variables or
conf/keycloak.conf. -
Optionally re-run build:
bin\kc.bat build [new-options]. -
Start the service:
net start keycloak.
25.6. Troubleshooting Copy linkLink copied to clipboard!
25.6.1. Access Denied errors Copy linkLink copied to clipboard!
- Ensure the service runs as Local System (default) or that the specified account has "Log on as a service".
25.6.2. Options defined as environment variables are ignored Copy linkLink copied to clipboard!
Windows Services run in a separate session (usually as the LocalSystem account) and do not inherit the environment variables of the user who created the service. Define the required KC_* environment variables as system-wide environment variables, so they are available to the service.
25.6.3. Forcefully terminate the service Copy linkLink copied to clipboard!
If the Apache Commons Daemon wrapper becomes unresponsive:
taskkill /f /im prunsrv.exe
Use caution — this will affect all Procrun-managed services on the host.
25.7. Apache Commons Daemon configuration under the hood Copy linkLink copied to clipboard!
When you create the service, the following Apache Commons Daemon Procrun settings are applied:
-
StartMode:
exe(runskc.batas an external process) -
StartImage:
<KEYCLOAK_HOME>\bin\kc.bat -
StartParams:
start -
StopMode:
exe -
StopImage:
<KEYCLOAK_HOME>\bin\kc.bat -
StopParams:
stop - StopTimeout: configurable (default: 30 seconds)
Service configuration is stored in the Windows Registry under:
HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Apache Software Foundation\ProcRun 2.0\<ServiceName>