I'm trying to configure Tomcat 8.5.37 with systemctl to start the server as a daemon on Ubuntu 16.04.
For that I created the following service file at /etc/systemd/system/tomcat8.service:
[Unit]
Description=Tomcat Server
After=network.target
[Service]
Type=forking
Environment=JAVA_HOME=/opt/jdk1.8
Environment=CATALINA_PID=/opt/tomcat8/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat8
Environment=CATALINA_BASE=/opt/tomcat8
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -Dlog4j.configurationFile=/opt/conf/log4j2.xml'
Environment='JAVA_OPTS=-Djava.net.preferIPv4Stack=true -Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'
ExecStart=/opt/tomcat8/bin/startup.sh
ExecStop=/opt/tomcat8/bin/shutdown.sh
User=tomcat
Group=tomcat
UMask=0007
#RestartSec=40
#Restart=always
[Install]
WantedBy=multi-user.target
The Tomcat server was downloaded directly from apache and only extracted. JDK is the latest Oracle JDK 1.8.
When I start the tomcat via startup.sh directly all is fine. But when I'm using systemctl (daemon-reload was done before) it crashs directly.
Output of systemctl status tomcat8:
● tomcat8.service - Tomcat Server
Loaded: loaded (/etc/systemd/system/tomcat8.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Fri 2019-01-04 23:14:08 CET; 2s ago
Process: 9793 ExecStop=/opt/tomcat8/bin/shutdown.sh (code=exited, status=0/SUCCESS)
Process: 9781 ExecStart=/opt/tomcat8/bin/startup.sh (code=exited, status=0/SUCCESS)
Main PID: 9791 (code=exited, status=0/SUCCESS)
Output of journalctl -xe:
Jan 04 23:14:04 myserver.com systemd[1]: Starting Tomcat Server...
-- Subject: Unit tomcat8.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit tomcat8.service has begun starting up.
Jan 04 23:14:04 myserver.com startup.sh[9781]: Tomcat started.
Jan 04 23:14:04 myserver.com systemd[1]: Started Tomcat Server.
-- Subject: Unit tomcat8.service has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit tomcat8.service has finished starting up.
--
-- The start-up result is done.
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: Jan 04, 2019 11:14:06 PM org.apache.catalina.startup.Catalina stopServer
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: SEVERE: Could not contact [localhost:[8005]]. Tomcat may not be running.
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: Jan 04, 2019 11:14:07 PM org.apache.catalina.startup.Catalina stopServer
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: SEVERE: Catalina.stop:
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: java.net.ConnectException: Connection refused (Connection refused)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at java.net.PlainSocketImpl.socketConnect(Native Method)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at java.net.Socket.connect(Socket.java:589)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at java.net.Socket.connect(Socket.java:538)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at java.net.Socket.<init>(Socket.java:434)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at java.net.Socket.<init>(Socket.java:211)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at org.apache.catalina.startup.Catalina.stopServer(Catalina.java:503)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at java.lang.reflect.Method.invoke(Method.java:498)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at org.apache.catalina.startup.Bootstrap.stopServer(Bootstrap.java:406)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:498)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: The stop command failed. Attempting to signal the process to stop through OS signal.
The exception is not the problem, because it tries to shutdown a service, that exists no longer.
What I can see is, that the log files will be created, which brings me to the point, that the start process has begun. But I'm confused by the processes at the status output. There is a process for start and a process for stop.
It looks like startup.sh starts to run, but the exit-code is wrong or not recognized and systemd runs the ExecStop command directly after startup.sh was executed.
Another server runs fine with that configuration, but here I have no chance to get it running till now.
Has anybody some ideas what's the problem in this case?
Thanks!