Spring boot application as a service + VM Options
Asked Answered
C

3

6

I have a Spring boot application that is started as a service using Linux's systemd.

It is based on this documentation : http://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html

With the default script, the jar file start. It works fine.

/etc/systemd/system/myapp.service :

[Unit]
Description=myapp
After=syslog.target

[Service]
User=myapp
ExecStart=/var/myapp/myapp.jar
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

Now I want to add VM Option when the jar start. I tried to add a .conf file to the project but it doesn't work.

/var/myapp/myapp.conf :

JAVA_OPTS=-Xms256M -Xmx512M

How can I add JVM option to start the application with systemd ?

Carty answered 29/7, 2016 at 14:50 Comment(0)
C
5

I finally found a solution here : how to configure heap size when start a spring-boot application with embedded tomcat?

The content of my .conf file was wrong. I need too write this :

export JAVA_OPTS="-Xms256m -Xmx512m"

Now when I run "service myapp start", it start with the good heap size.

Carty answered 1/8, 2016 at 12:9 Comment(0)
T
2

According to the documentation you can simply add an environment variable JAVA_OPTS if that's enough for you.

The way we start the apps with custom ENV variables and systemd would look like this for your project:

[Unit]
Description=myapp
After=syslog.target

[Service]
User=myapp
ExecStart=source /var/myapp/myapp.conf; java -jar /var/myapp/myapp.jar
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

Basically sourcing the ENV config directly will expose the ENV variables to the application

Tontine answered 29/7, 2016 at 22:11 Comment(1)
Thanks for your answer. I tried this but it doesn't work for me.Carty
D
2

Another good way is to create a configuration file myapp.conf in the same directory with the myapp.jar file

/var/myapp/

and put the following content:

JAVA_OPTS="-Xms256m -Xmx512m"

Then restart the application.

Note that the name of configuration file should be the same with the jar file except the extension, .conf instead of .jar or .war.

This way have some other advantages:

  • Keep the settings separately for the myapp application only.
  • We can have additional parameters in the file too.

More detail can be found here.

Drambuie answered 30/5, 2020 at 5:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.