You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu服务器中Tomcat退出码失败及状态异常问题解决

Ubuntu服务器上Tomcat退出码失败与状态异常的排查方案

Hey there, let's tackle your Tomcat issues on Ubuntu step by step. Looking at your systemctl status output, it says Tomcat is active (running), but you're hitting exit code failures and status anomalies—so we need to dig past the surface status to find the real problem.

一、排查Tomcat退出码失败的核心原因

Exit code failures usually mean Tomcat crashed at some point (even if systemd auto-restarted it to show "running" now). Here's what to check first:

  • Dive into Tomcat logs immediately
    This is the most critical step. Default logs live in /var/lib/tomcat/logs/ (or your custom CATALINA_BASE logs directory). Focus on catalina.out (the main runtime log) and localhost.log—look for ERROR or SEVERE entries, which point to crashes from things like out-of-memory errors, missing dependencies, or misbehaving web apps.
    Run this to grab recent log entries:

    sudo tail -n 100 /var/lib/tomcat/logs/catalina.out
    
  • Check JVM memory settings
    A common cause of exit code failures is insufficient memory. Open your Tomcat systemd config file:

    sudo nano /etc/systemd/system/tomcat.service
    

    Look for the JAVA_OPTS line—if the -Xmx (max heap) value is too low (like -Xmx512m for a memory-heavy app), bump it up. For example:

    Environment="JAVA_OPTS=-Djava.awt.headless=true -Xmx1024m -Xms512m"
    

    Save the file, then reload systemd and restart Tomcat:

    sudo systemctl daemon-reload
    sudo systemctl restart tomcat
    
  • Verify file permissions
    Make sure the Tomcat user (usually tomcat) has read/write access to all Tomcat directories (installation, webapps, logs). Fix permissions with:

    sudo chown -R tomcat:tomcat /var/lib/tomcat/
    

二、修复systemctl状态异常问题

Even though systemctl status says "active (running)", you're seeing issues—here's why and how to fix it:

  • 检查Tomcat是否真的在监听端口
    有时候进程存在,但服务器无法接受连接。验证默认的8080端口是否在使用:

    sudo netstat -tulpn | grep java
    

    如果看不到8080端口,说明Tomcat启动失败——回到catalina.out日志查找原因。

  • 刷新systemd的状态缓存
    Systemd有时会显示过时的状态信息。强制重载并重启:

    sudo systemctl daemon-reload
    sudo systemctl restart tomcat
    

    然后重新检查状态:

    sudo systemctl status tomcat
    
  • 修复不匹配的PID文件
    Tomcat会创建一个PID文件(通常在/var/run/tomcat.pid)来跟踪进程。如果这个文件里的PID和状态输出中的Main PID(20422)不一致,说明进程异常。删除PID文件并重启:

    sudo rm /var/run/tomcat.pid
    sudo systemctl restart tomcat
    

三、额外的排查技巧

  • 手动启动Tomcat查看实时错误
    绕过systemd,使用Tomcat原生启动脚本捕获即时问题:

    sudo su - tomcat -c "/usr/share/tomcat/bin/startup.sh"
    

    然后查看实时日志输出:

    tail -f /var/lib/tomcat/logs/catalina.out
    

    这会让你清楚看到启动过程中到底哪里出错了。

  • 确认JDK兼容性
    Tomcat 8需要Java 7或更高版本。你的状态显示使用的是java-8-openjdk-amd64(没问题),但可以再确认一下Java版本:

    java -version
    

内容的提问来源于stack exchange,提问作者Vel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:20:16