Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

“Invest in yourself — your confidence is always worth it.”

Explore Cosmetic Hospitals

Start your journey today — compare options in one place.

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

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals

Similar Posts

  • What is Duo and use cases of Duo?

    What is Duo? Duo, now a part of Cisco, is a multi-factor authentication (MFA) and access security platform that helps organizations protect their applications and data by verifying the identity of users and devices. It adds an extra layer of security by essential users to provide multiple forms of verification before granting access. Duo’s versatile…

  • How to save HTTP requests headers, methods and body to a file?

    Method 1 – Using mod_log_forensic apache Module Step 1 – Check if mod_log_forensic module is enabled or not Step 2 – Enable mod_log_forensic module using following lines in httpd.conf Method 2 – Using PHP Script Rajesh KumarI’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at Cotocus. I share tech blog…

  • Joomla Website Performance

    SEO and PERFORMANCE http://www.joomlaaa.com/Joomla-Articles/optimization/How-can-I-make-my-Joomla-site-load-faster.html http://www.joomlaperformance.com http://www.websiteoptimization.com http://www.pathos-seo.com/joomla-technical-optimization/making-a-faster-joomla-website.html http://www.pathos-seo.com/joomla-technical-optimization/making-a-faster-joomla-website.html   http://www.wzcreativetechnology.com/joomla-tips-and-tricks/74-how-to-make-joomla-site-load-fast.html http://extensions.joomla.org/extensions/site-management/cache/7350 http://www.open-source-depot.com/archives/13-MySQL-Performance-Tip-on-CentOS4-on-GoDaddy.html http://www.open-source-depot.com/archives/13-MySQL-Performance-Tip-on-CentOS4-on-GoDaddy.html http://www.cyberciti.biz/tips/enable-the-query-cache-in-mysql-to-improve-performance.html   http://forum.joomla.org/viewtopic.php?f=433&t=344013l http://kb.siteground.com/article/How_to_optimize_a_MySQL_database_using_phpMyAdmin.html http://www.electrictoolbox.com/mysql-optimize-table-cli/ http://forum.joomla.org/viewtopic.php?p=1250059 http://kb.siteground.com/article/How_to_optimize_a_MySQL_database.html http://developer.yahoo.com/performance/rules.html http://www.mightyjoomla.com/Joomla-Tips-Tricks/enable-and-use-gzip-compression-in-joomla-cms-to-enhance-website-performance.html http://forum.joomla.org/viewtopic.php?t=50278     www.websiteoptimization.com/services/analyze/   http://forum.joomla.org/viewtopic.php?t=50278 http://www.phpmagicbook.com/htaccess-essential-tricks-for-blog-website-performance-a-guide/ http://www.dquinn.net/htaccess-adding-etags-gzip-expires-headers/ Mantosh SinghMotoShare.in delivers cost-effective bike rental solutions, empowering users to save on transportation while enjoying reliable two-wheelers. Ideal for city commutes, sightseeing, or adventure rides….

  • Top 10 Data Migration Tools in 2026: Features, Pros, Cons & Comparison

    Introduction In today’s increasingly digital world, businesses are continually evolving their IT infrastructure. Whether upgrading systems, moving to the cloud, or consolidating data, Data Migration Tools have become critical in ensuring the seamless transfer of data across environments. Data migration is the process of transferring data between storage types, formats, or systems, and in 2026,…

  • What is Bitcoin?

    What the Heck is Bitcoin Anyway? Have you ever heard of Bitcoin? If you haven’t, then you’re missing out, my friend. It’s like this whole new world of digital currency that’s taking the world by storm. But what is it exactly? Let’s dive in and find out. The Basics of Bitcoin Bitcoin is a decentralized…

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments