Short Story:
When running 'sudo systemctl start tomcat', it seems to call shutdown.sh immediately after startup.sh:
Feb 03 19:36:49 xxxxx startup.sh[10285]: Tomcat started.
Feb 03 19:36:49 xxxxx shutdown.sh[10294]: NOTE: Picked up JDK_JAVA_OPTIONS: --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: Feb 03, 2019 7:36:53 PM org.apache.catalina.startup.Catalina stopServer
Whereas if I manually call startup.sh then it's fine.
Long Story:
I have set up downloaded and installed Tomcat with this script:
#Add Tomcat group
sudo groupadd tomcat
#Add Tomcat user, creating a home directory.
sudo useradd -m -g tomcat -d /opt/tomcat -s /bin/nologin tomcat
#Move to temp directory
cd /tmp
#Get Tomcat tar file
wget https://www-eu.apache.org/dist/tomcat/tomcat-9/v9.0.14/bin/apache-
tomcat-9.0.14.tar.gz
#Extract tar file to /opt/tomcat folder
tar -zxvf apache-tomcat-9.0.14.tar.gz -C /opt/tomcat/ --strip-components=1
#Give tomcat group recursive ownership of the installation directory
sudo chgrp -R tomcat /opt/tomcat
cd /opt/tomcat
#Give tomcat user execute access and reursive read access to conf
sudo chmod -R g+r conf
sudo chmod g+x conf
#Make tomcat user ownership of required directories
sudo chown -R tomcat webapps/ work/ temp/ logs/
#Make scripts in bin executable
sudo chmod +x /opt/tomcat/bin/*.sh
I added this file to /etc/systemd/system/tomcat.service:
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/local/jdk-11.0.1
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/bin/kill -15 $MAINPID
User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always
[Install]
WantedBy=multi-user.target
Then if I run it like this:
systemctl daemon-reload
systemctl enable tomcat
systemctl start tomcat
It fails to start.
I get this output from 'systemctl status tomcat -l':
tomcat.service - Apache Tomcat Web Application Container
Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: disabled)
Active: activating (auto-restart) (Result: exit-code) since Sun 2019-02-03 12:49:15 UTC; 3s ago
Process: 9767 ExecStop=/bin/kill -15 $MAINPID (code=exited, status=1/FAILURE)
Process: 9754 ExecStart=/opt/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
Main PID: 9765 (code=exited, status=0/SUCCESS)
Feb 03 12:49:15 xxxxx systemd[1]: Unit tomcat.service entered failed state.
Feb 03 12:49:15 xxxxx systemd[1]: tomcat.service failed.
and 'journalctl -u tomcat.service -l' returns:
Feb 03 12:48:12 xxxxx systemd[1]: Starting Apache Tomcat Web Application Container...
Feb 03 12:48:12 xxxxx startup.sh[9661]: Existing PID file found during start.
Feb 03 12:48:13 xxxxx startup.sh[9661]: Removing/clearing stale PID file.
Feb 03 12:48:13 xxxxx systemd[1]: Started Apache Tomcat Web Application Container.
Feb 03 12:48:13 xxxxx systemd[1]: tomcat.service: control process exited, code=exited status=1
Feb 03 12:48:13 xxxxx systemd[1]: Unit tomcat.service entered failed state.
Feb 03 12:48:13 xxxxx systemd[1]: tomcat.service failed.
If I add the file '/opt/tomcat/bin/setenv.sh' with content:
export JAVA_HOME=/usr/local/jdk-11.0.1
Then if I run:
sudo /opt/tomcat/bin/startup.sh
Then Tomcat starts up fine. Does this suggest something is wrong with my tomcat.service file?
The OS is Centos 7.
Edit 1:
Another thing worth mentioning: running startup.sh as tomcat user the command:
sudo -u tomcat /opt/tomcat/bin/startup.sh
Runs everything fine, so I don't think it's a problem with the user like permissions.
Edit 2:
I think the PID errors were misleading. I changed the ExecStop line of '/etc/systemd/system/tomcat.service' to:
ExecStop=/opt/tomcat/bin/shutdown.sh
Now the output of 'journalctl -u tomcat.service -l' looks like:
Feb 03 19:36:48 xxxxx systemd[1]: Started Apache Tomcat Web Application Container.
Feb 03 19:36:48 xxxxx systemd[1]: Starting Apache Tomcat Web Application Container...
Feb 03 19:36:49 xxxxx startup.sh[10285]: Tomcat started.
Feb 03 19:36:49 xxxxx shutdown.sh[10294]: NOTE: Picked up JDK_JAVA_OPTIONS: --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: Feb 03, 2019 7:36:53 PM org.apache.catalina.startup.Catalina stopServer
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: SEVERE: Could not contact [localhost:8005] (base port [8005] and offset [0]). Tomcat may not be running.
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: Feb 03, 2019 7:36:54 PM org.apache.catalina.startup.Catalina stopServer
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: SEVERE: Error stopping Catalina
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: java.net.ConnectException: Connection refused (Connection refused)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at java.base/java.net.Socket.connect(Socket.java:591)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at java.base/java.net.Socket.connect(Socket.java:540)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at java.base/java.net.Socket.<init>(Socket.java:436)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at java.base/java.net.Socket.<init>(Socket.java:213)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at org.apache.catalina.startup.Catalina.stopServer(Catalina.java:513)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at java.base/java.lang.reflect.Method.invoke(Method.java:566)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at org.apache.catalina.startup.Bootstrap.stopServer(Bootstrap.java:403)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:497)