Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An example configuration for Tomcat as systemd service. #tomcat #systemd #systemd.service

README

Let Tomcat is download and installed under /opt/tomcat. Also, let tomcat be a non-provileged user under which the server will be running.

We assume that we keep server's binaries under /opt/tomcat and we will create a server instance named foo under /var/tomcat/ (carrying its own conf, logs, webapps, work, lib directories). See also https://dzone.com/articles/running-multiple-tomcat.

Create a template service unit file at /etc/systemd/system/tomcat@.service:

[Unit]
Description=Tomcat - instance %i
After=syslog.target network.target

[Service]
Type=forking

User=tomcat
Group=tomcat

WorkingDirectory=/var/tomcat/%i

Environment="JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"

Environment="CATALINA_PID=/var/tomcat/%i/run/tomcat.pid"
Environment="CATALINA_BASE=/var/tomcat/%i/"
Environment="CATALINA_HOME=/opt/tomcat/"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"

ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh

#RestartSec=10
#Restart=always

[Install]
WantedBy=multi-user.target

Now, we can instantiate a service instance for our foo tomcat instance:

systemctl daemon-reload
systemctl enable tomcat@foo.service
systemctl start tomcat@foo.service
@dimka310

This comment has been minimized.

Copy link

@dimka310 dimka310 commented May 18, 2018

Hi, Alex! Nice solution :) Can you suggest how to configure systemctl service for 2 different Tomcat instances on one server (e.g. 2 different Catalina Base folders). In this case ExecStart path will be different.

@drmalex07

This comment has been minimized.

Copy link
Owner Author

@drmalex07 drmalex07 commented Jun 17, 2018

@dimka310 I suppose that you need 2 separate service unit files (you cannot benefit from using service templates)

@supamanda

This comment has been minimized.

Copy link

@supamanda supamanda commented Jul 23, 2018

In order to run 2 separate services:

  • in tomcat.service update the ExecStart and ExecStop to point to the <tomcat1_home>/bin/
  • in tomcat2.service update the ExecStart and ExecStop to point to the <tomcat2_home>/bin/

And in both remove all of the Environment lines because you can't share the CATALINA variables. I used a setenv.sh file in the <tomcat_home>/bin folder to set any java opts.

@rcrathore

This comment has been minimized.

Copy link

@rcrathore rcrathore commented Jul 27, 2018

@supamanda it is not correct. the startup.sh and shutdown.sh file remain in CATALINA_HOME/bin. The setenv.sh is kept in CATALINA_BASE/bin.

@sardar-ji

This comment has been minimized.

Copy link

@sardar-ji sardar-ji commented Jan 13, 2019

Hi Alex, Is it mandatory to give CATALINA_HOME and BASE path? I have just given ExecStart and ExectStop values, however it is not working as expected

@mahsandu

This comment has been minimized.

Copy link

@mahsandu mahsandu commented Dec 10, 2019

Thanks a lot to share. I am running webmin apache tomcat plugin: I have modified like below it is working:

[Unit]
Description=Tomcat - instance %i
After=syslog.target network.target

[Service]
Type=forking

User=tomcat
Group=tomcat

ExecStart=$CATALINA_HOME/bin/startup.sh
ExecStop=$CATALINA_HOME/bin/shutdown.sh

RestartSec=10
Restart=always

@honsberg

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.