🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

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;
3.
4. if (status is “not running”) {
5. start tomcat;
6. }
7. }
[/code]

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
[/code]

To run the script every 10 minutes:

1. Save the above script to “/root/bin/recover-tomcat.sh”.

2. Add execute permission:

1. chmod +x /root/bin/recover-tomcat.sh

chmod +x /root/bin/recover-tomcat.sh

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/recover-tomcat.sh

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. }
[/code]

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
3.
4. if [ -f $TOMCAT_HOME/bin/tomcat.pid ]
5. then
6. echo “PID file exists”
7. pid=”`cat $TOMCAT_HOME/bin/tomcat.pid`”
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/startup.sh
14. fi
15. else
16. echo “PID file does not exist. Restarting…”
17. $TOMCAT_HOME/bin/startup.sh
18. fi
[/code]

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.

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Subscribe
Notify of
guest


0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x