Skip to content

Instantly share code, notes, and snippets.

@drmalex07
Last active October 22, 2025 22:03
Show Gist options
  • Save drmalex07/e6e99dad070a78d5dab24ff3ae032ed1 to your computer and use it in GitHub Desktop.
Save drmalex07/e6e99dad070a78d5dab24ff3ae032ed1 to your computer and use it in GitHub Desktop.
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/[email protected]:

[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 [email protected]
systemctl start [email protected]
@v-bulynkin
Copy link

v-bulynkin commented Oct 22, 2025

Systemd also allows to make unprivileged user's services, very convenient. No need to use sudo, etc.

# Create or edit service. You shouldn't execute "daemon-reload" after systemctl edit command
systemctl edit tomcat --user --full --force

Paste something like this:

[Unit]
Description=Tomcat
After=network.target

[Service]
# The "exec" type is better than "simple"
Type=exec
EnvironmentFile=-/etc/default/tomcat
SyslogIdentifier=tomcat
ExecStart=/usr/bin/java $JAVA_OPTS $CATALINA_OPTS org.apache.catalina.startup.Bootstrap start
ExecStop=/usr/bin/java $JAVA_OPTS org.apache.catalina.startup.Bootstrap stop
Restart=on-failure

[Install]
# For user's services, write "default.target", not "multi-user.target"
WantedBy=default.target

Exec type
default.target

By default, user's services work only when the user is logged-in. To allow user services working independently from logon, turn on lingering:

# It needs to be done only once
loginctl enable-linger $USER

# Check status (Linger: yes)
loginctl user-status

To control such services, add --user parameter to systemctl.

# Start and enable service
systemctl --user enable --now tomcat

# Status
systemctl --user status tomcat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment