Slide 1
Most trusted JOB oriented professional program
DevOps Certified Professional (DCP)

Take your first step into the world of DevOps with this course, which will help you to learn about the methodologies and tools used to develop, deploy, and operate high-quality software.

Slide 2
DevOps to DevSecOps – Learn the evolution
DevSecOps Certified Professional (DSOCP)

Learn to automate security into a fast-paced DevOps environment using various open-source tools and scripts.

Slide 2
Get certified in the new tech skill to rule the industry
Site Reliability Engineering (SRE) Certified Professional

A method of measuring and achieving reliability through engineering and operations work – developed by Google to manage services.

Slide 2
Master the art of DevOps
Master in DevOps Engineering (MDE)

Get enrolled for the most advanced and only course in the WORLD which can make you an expert and proficient Architect in DevOps, DevSecOps and Site Reliability Engineering (SRE) principles together.

Slide 2
Gain expertise and certified yourself
Azure DevOps Solutions Expert

Learn about the DevOps services available on Azure and how you can use them to make your workflow more efficient.

Slide 3
Learn and get certified
AWS Certified DevOps Professional

Learn about the DevOps services offered by AWS and how you can use them to make your workflow more efficient.

previous arrow
next arrow

How to automatically recover Tomcat from crashes

rajeshkumar created the topic: How to automatically recover Tomcat from crashes

How to automatically recover Tomcat from crashes

Tomcat occasionally crashes if you do frequent hot-deploys or if you are running it on a machine with low memory. Every time tomcat crashes someone has to manually restart it, so I wrote a script which automatically detects that tomcat has crashed and restarts it.

Here’s the pseudo logic:

[code language=”css”]
1. every few minutes {
2. check tomcat status;
4. if (status is “not running”) {
5. start tomcat;
6. }
7. }

Here’s a shell script to implement the above logic. It assumes that you are running on a unix/linux system and have /etc/init.d/tomcat* script setup to manage tomcat.

Adjust the path to “/etc/init.d/tomcat” in the script below to reflect the correct path on your computer. Sometimes it is called /etc/init.d/tomcat5 or /etc/init.d/tomcat6 depending on your tomcat version. Also make sure that the message “Tomcat Servlet Container is not running.” matches with the message that you get when you run the script when tomcat is stopped.

[code language=”css”]
# #! /bin/sh
# SERVICE=/etc/init.d/tomcat
# STOPPED_MESSAGE=”Tomcat Servlet Container is not running.”
# if [ “`$SERVICE status`” == “$STOPPED_MESSAGE”];
# then
# {
# $SERVICE start
# }
# fi

To run the script every 10 minutes:

1. Save the above script to “/root/bin/”.

2. Add execute permission:

1. chmod +x /root/bin/

chmod +x /root/bin/

3. Add this to root’s crontab, type the following as root:

1. crontab -e

crontab -e

4. Add the following lines to the crontab:

1. # monitor tomcat every 10 minutes
2. */10 * * * * /root/bin/

What if I don’t have /etc/init.d/tomcat* script on my computer?

Tomcat creates a pid file, typically in the TOMCAT_HOME/bin directory. This file contains the process id of the tomcat process running on the machine. The pseudo logic in that case would be:

[code language=”css”]
1. if (the PID file does not exist) {
2. // conclude that tomcat is not running
3. start tomcat
4. }
5. else {
6. read the process id from the PID file
7. if (no process that id is running) {
8. // conclude that tomcat has crashed
9. start tomcat
10. }
11. }

You can implement the above logic as follows. The following is experimental and is merely a suggested way, test it on your computer before using it.

[code language=”css”]
1. # adjust this to reflect tomcat home on your computer
2. TOMCAT_HOME=/opt/tomcat5
4. if [ -f $TOMCAT_HOME/bin/ ]
5. then
6. echo “PID file exists”
7. pid=”`cat $TOMCAT_HOME/bin/`”
8. if [ “X`ps -p $pid | awk ‘{print $1}’ | tail -1`” = “X”]
9. then
10. echo “Tomcat is running”
11. else
12. echo “Tomcat had crashed”
13. $TOMCAT_HOME/bin/
14. fi
15. else
16. echo “PID file does not exist. Restarting…”
17. $TOMCAT_HOME/bin/
18. fi

Why would tomcat crash?

The most common reason is low memory. For example, if you have allocated 1024MB of max memory to tomcat and enough memory is not available on that machine. Other reasons may involve repeated hot-deploys causing memory leaks, rare JVM bugs causing the JVM to crash.

Rajesh Kumar
Twitt me @

Rajesh Kumar
0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x