systemctl stops Tomcat service immediately after start

Thanks @MichaelHampton for the good article: https://jdebp.eu/FGA/systemd-house-of-horror/tomcat.html

It explains the possible problems with a typical installation suggested in the tutorial I read (and tons of other copy-pasted ones). I could not figured out why the problem arises. Maybe it is the problem with all of the layers introduced by catalina.sh, startup.sh, etc.

So basically, I rewrote the systemd config script and it started to work.


I had the exact same problem. Only I was using Amazon Lightsail. Maybe there is a problem with the way that Amazon configures their Ubuntu machines, and the described way to create a Tomcat Service. I used the described way on a local Ubuntu machine that I set up myself and that worked fine.

I read the article by @michael-hampton and used the approach described there. I am a Linux noob, and I found that I had some problems getting it to work. I'm including the files I ended up with here so maybe other Linux noobs that have this problem can get an easier start. My Tomcat installation is at /opt/tomcat and these are the files I ended up with:

/etc/systemd/system/tomcat.service

[Unit]
Description=Apache Tomcat Web Application Container
After=network.target


[Service]
User=tomcat
Group=tomcat
UMask=0007

EnvironmentFile=/etc/default/tomcat
ExecStart=/usr/bin/env ${JAVA_HOME}/bin/java \
$JAVA_OPTS $CATALINA_OPTS \
-classpath ${CLASSPATH} \
-Dcatalina.base=${CATALINA_BASE} \
-Dcatalina.home=${CATALINA_HOME} \
-Djava.endorsed.dirs=${JAVA_ENDORSED_DIRS} \
-Djava.io.tmpdir=${CATALINA_TMPDIR} \
-Djava.util.logging.config.file=${CATALINA_BASE}/conf/logging.properties \
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager \
org.apache.catalina.startup.Bootstrap \
start

ExecStop=/usr/bin/env ${JAVA_HOME}/bin/java \
$JAVA_OPTS $CATALINA_OPTS \
-classpath ${CLASSPATH} \
-Dcatalina.base=${CATALINA_BASE} \
-Dcatalina.home=${CATALINA_HOME} \
-Djava.endorsed.dirs=${JAVA_ENDORSED_DIRS} \
-Djava.io.tmpdir=${CATALINA_TMPDIR} \
-Djava.util.logging.config.file=${CATALINA_BASE}/conf/logging.properties \
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager \
org.apache.catalina.startup.Bootstrap \
stop

SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

/etc/default/tomcat

CATALINA_HOME=/opt/tomcat
CATALINA_BASE=/opt/tomcat
CATALINA_TMPDIR=/opt/tomcat/temp
CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC
JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom
JAVA_ENDORSED_DIRS=/opt/tomcat/endorsed
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
CLASSPATH=/opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar