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 customCATALINA_BASElogs directory). Focus oncatalina.out(the main runtime log) andlocalhost.log—look forERRORorSEVEREentries, 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.outCheck 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.serviceLook for the
JAVA_OPTSline—if the-Xmx(max heap) value is too low (like-Xmx512mfor 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 tomcatVerify file permissions
Make sure the Tomcat user (usuallytomcat) 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

