# Startup keycloak as a permanent runing service

**URL:** <https://forum.keycloak.org/t/startup-keycloak-as-a-permanent-runing-service/8206>\
**Category:** Getting advice\
**Created:** [March 30, 2021, 10:40am UTC](https://forum.keycloak.org/t/startup-keycloak-as-a-permanent-runing-service/8206 "2021-03-30T10:40:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![xkey](https://avatars.discourse-cdn.com/v4/letter/x/ce7236/32.png) [@xkey](https://forum.keycloak.org/u/xkey)\
**Post date:** [March 30, 2021, 10:40am UTC](https://forum.keycloak.org/t/startup-keycloak-as-a-permanent-runing-service/8206/1 "2021-03-30T10:40:00Z")

</div>

Hello everybody,

i started with the installation of keycloak as a standalone server based on the zip file with wildfly.

But i have run into problems to start the service keycloak permanently as a service or behind a reverse proxy like apache or nginx

How can I start keycloak permanently without having to start the file manually?

Here I have already tested the following configuration but unfortunately the service keycloak does not start

/etc/systemd/system/keycloak.service

_[Unit]_  
_Description=Keycloak_  
_After=network.target_

- 
  - 

_[Service]_  
_Type=idle_  
_User=keycloak_  
_Group=keycloak_  
_ExecStart=KEYCLOAK\_HOME/bin/standalone.sh -b 0.0.0.0_  
_TimeoutStartSec=600_  
_TimeoutStopSec=600_

- 
  - 

_[Install]_  
_WantedBy=multi-user.target_

And the error message from the service “keycloak.service”:

 ![grafik](https://global.discourse-cdn.com/free1/uploads/keycloak/original/2X/0/0606aed2bfdcd79ca5fb7400163837943ee22c42.png)

Maybe someone here can help me

Many thanks for your help

Using Keycloak 12.0.4 (Distribution powered by WildFly) on Cent OS 7

---

<div class="post-metadata">

**Author:** ![dasniko](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/dasniko/32/2969_2.png) [@dasniko](https://forum.keycloak.org/u/dasniko)\
**Post date:** [March 30, 2021, 12:12pm UTC](https://forum.keycloak.org/t/startup-keycloak-as-a-permanent-runing-service/8206/2 "2021-03-30T12:12:56Z")

</div>

> [@How to start keycloak automatically on server reboot](http://forum.keycloak.org/t/how-to-start-keycloak-automatically-on-server-reboot/7975):
>
> How can i automate this so it runs when ubuntu starts. cd /otp/keycloak/bin sudo nohup ./standalone.sh -0.0.0.0 bmanagement 0.0 offset = 100

---

<div class="post-metadata">

**Author:** ![xkey](https://avatars.discourse-cdn.com/v4/letter/x/ce7236/32.png) [@xkey](https://forum.keycloak.org/u/xkey)\
**Post date:** [March 30, 2021, 12:44pm UTC](https://forum.keycloak.org/t/startup-keycloak-as-a-permanent-runing-service/8206/3 "2021-03-30T12:44:00Z")

</div>

Thanks for your reply @dasniko

I will have a look at the sample files in the ZIP directory “…\keycloak-12.0.4\docs\contrib…” and get back to you otherwise if necessary

---

<div class="post-metadata">

**Author:** ![DavidR](https://avatars.discourse-cdn.com/v4/letter/d/3ab097/32.png) [@DavidR](https://forum.keycloak.org/u/DavidR)\
**Post date:** [December 8, 2021, 4:24pm UTC](https://forum.keycloak.org/t/startup-keycloak-as-a-permanent-runing-service/8206/4 "2021-12-08T16:24:42Z")

</div>

I’m replying to this because it’s the second hit on google for “keycloak systemd”

I did a lot of research on starting keycloak standalone using systemd, including the reviewing the information keycloak-15.0.1/docs/contrib/scripts. One note about the contrib scripts: they appear to be from the wildfly project and not specific to keycloak.

I Basically used the OP’s service file with a couple of changes:

```auto
[Unit]
Description=Keycloak Service
After=network.target

[Service]
User=keycloak
Group=keycloak
ExecStart=/opt/keycloak-15.0.1/bin/standalone.sh
WorkingDirectory=/opt/keycloak-15.0.1

[Install]
WantedBy=multi-user.target

```

I found the Type=idle and Timeout\* fields did not appear to be needed. I think the key attribute is the WorkingDirectory= setting. I read the standalone.sh script and it does quite a few things based on the current directory.

Notes:

1. I’m unpacking keycloak into /opt
2. I created the keycloak user and group, and chown’d /opt/keycloak-15.0.1 with this user and group.
3. I’m not overriding the default of binding to 127.0.0.1 because I’m using nginx to reverse proxy, but that should be a simple change to the ExecStart= setting.

The above script is working for me on CentOS, and both starts and stops keycloak, and I have it enabled and it starts keycloak at boot.

HTH anyone who comes after me.

---

<div class="post-metadata">

**Author:** ![EML-github](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/eml-github/32/9594_2.png) [@EML-github](https://forum.keycloak.org/u/EML-github)\
**Post date:** [January 21, 2024, 12:02pm UTC](https://forum.keycloak.org/t/startup-keycloak-as-a-permanent-runing-service/8206/5 "2024-01-21T12:02:59Z")

</div>

My 2p:

1. The ‘WorkingDirectory’ isn’t required
2. Keycloak normally exits with code 143, which looks like a problem in the systemctl and journalctl output. Avoid this by adding ‘SuccessExitStatus=0 143’ in the ‘Service’ section
3. Add ‘–optimized’ to the ExecStart line
4. The default ‘Restart’ is no, so your service never restarts if it dies. This isn’t great: consider ‘Restart=always’
5. You’ve set owner ‘keycloak:keycloak’ for the entire /opt/keycloak tree. This sounds like a really bad idea. My setup works fine with only /opt/keycloak/data owned by keycloak:keycloak, with everything else being owned by root:root

My setup: Keycloak 23, Ubuntu 22.04, Apache reverse proxy.
