Search code examples
javaspringspring-boottomcatsystemctl

Tomcat not loading using systemctl service


I've installed Tomcat 8.5 on my Ubuntu 22.04 machine. I'm able to test tomcat on localhost:8080 using startup.sh file and systemctl service. But, the moment I'm deploying my application's war file on tomcat, I'm not able to connect to localhost:8080. systemctl status tomcat is showing active.

When I'm manually starting my tomcat using startup.sh, I'm able to connect to both localhost:8080 and localhost:8080/myapp.

After comparing catalina.out logs for both case, I found that on manually starting tomcat, my log file is getting the following log:

Configuring Spring Security Core ... ... finished configuring Spring Security Core

And I'm able to connect to my application using tomcat. But, whenever I'm using systemctl to start tomcat, tomcat is getting started, but I'm never able to access it. In this case, my log file is not having the Spring Security Core line.

Here is my systemctl file:

[Unit]
Description=Tomcat 8.5
After=syslog.target network.target

[Service]
Type=forking

User=tomcat
Group=tomcat


Environment="JAVA_HOME=/home/myuser/.sdkman/candidates/java/8.0.282.hs-adpt"
Environment="JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom"
Environment="CATALINA_PID=/opt/tomcat/temp/tomcat.pid"
Environment="CATALINA_BASE=/opt/tomcat"
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

There is no issue related to tomcat user permission and ownership as I'm able to start and connect to my application using the following command:

sudo -u tomcat bash -c '/opt/tomcat/bin/startup.sh'

Edit:

journalctl output:

An ExecStart= process belonging to unit tomcat.service has exited. 
    ░░ ░░ The process' exit code is 'exited' and its exit status is 1.
     Aug 21 14:44:49 myMachine systemd[1]: tomcat.service: Failed with result 'exit-code'.
     ░░ Subject: Unit failed ░░ Defined-By: systemd ░░ 
    Support: http://www.ubuntu.com/support 
    ░░ ░░ The unit tomcat.service has entered the 'failed' state with result 'exit-code'. 
    Aug 21 14:44:49 myMachine systemd[1]: Failed to start Tomcat - instance . 
    ░░ Subject: A start job for unit tomcat.service has failed ░░ 
    Defined-By: systemd ░░ Support: http://www.ubuntu.com/support
     ░░ ░░ A start job for unit tomcat.service has finished with a failure. ░░ ░░ 
    The job identifier is 14172 and the job result is failed.

Solution

  • I forced my tomcat to give me an error on journaling and make this steps and works here (Ubuntu 22.04)

    1. Add a tomcat user (if you have skipp this)

      sudo useradd -m -d /opt/tomcat -U -s /bin/false tomcat
      
    2. Grant permissions

      sudo chown -R tomcat:tomcat /opt/tomcat/
      
      sudo chmod -R u+x /opt/tomcat/bin
      
    3. Edit my systemd

      [Unit]
      Description=Tomcat
      After=network.target
      
      [Service]
      Type=forking
      
      User=tomcat
      Group=tomcat
      
      Environment="JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64"
      Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"
      Environment="CATALINA_BASE=/opt/tomcat"
      Environment="CATALINA_HOME=/opt/tomcat"
      Environment="CATALINA_PID=/opt/tomcat/temp/tomcat.pid"
      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
      
    4. Reload the systemd daemon

      sudo systemctl daemon-reload
      
    5. Try to start

      sudo systemctl start tomcat
      

    After this work fine to me.