How to create a Tomcat unit in systemd

Sometime we need to install Tomcat from sources. Also, to restart or stop the tomcat service we need to create a systemd unit.This implementation is working on CentOS and RedHat systems, also it’s working on any other linux distribution that has systemd, but file location can differ.

Systemd unit for Tomcat

We need to create the following file in /usr/lib/systemd/system/tomcat8.service (file name can be any other name):

# Systemd unit file for tomcat
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target

[Service]
Type=forking

Environment=JAVA_HOME=/opt/java/
Environment=CATALINA_PID=//opt/tomcat8/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat8
Environment=CATALINA_BASE=/opt/tomcat8
Environment='CATALINA_OPTS=-Xms3096M -Xmx4096M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Dfile.encoding=UTF-8' 

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

User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always
LimitNOFILE=150000

[Install]
WantedBy=multi-user.target

Systemd daemon

After we’ve done the tomcat unit file we have to:

Reload the daemon

systemctl daemon-reload

Enable the service

systemctl enable tomcat8

Don’t forget to

  1. Install JAVA
  2. Make tomcat startup scripts executable
  3. Make tomcat base owned by user tomcat or your user