{"id":3212,"date":"2010-06-28T20:51:03","date_gmt":"2010-06-29T00:51:03","guid":{"rendered":"http:\/\/g33kinfo.com\/info\/?p=3212"},"modified":"2010-06-28T20:51:03","modified_gmt":"2010-06-29T00:51:03","slug":"automatically-recover-tomcat-from-crashes","status":"publish","type":"post","link":"https:\/\/g33kinfo.com\/info\/automatically-recover-tomcat-from-crashes\/","title":{"rendered":"Automatically recover Tomcat from crashes"},"content":{"rendered":"<p>From <a href=\"http:\/\/www.vineetmanohar.com\/2010\/06\/howto-auto-recover-tomcat-crashes\/\" target=\"_blank\" rel=\"noopener noreferrer\">vineetmanohar.com<\/a><\/p>\n<p>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.<\/p>\n<p>Here\u2019s the pseudo logic:<\/p>\n<p>every few minutes<br \/>\n<code><br \/>\n{<br \/>\n  check tomcat status;<\/p>\n<p>  if (status is \"not running\") {<br \/>\n    start tomcat;<br \/>\n  }<br \/>\n}<br \/>\n<\/code><br \/>\nHere\u2019s 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.<\/p>\n<p>Adjust the path to \u201c\/etc\/init.d\/tomcat\u201d 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 \u201cTomcat Servlet Container is not running.\u201d matches with the message that you get when you run the script when tomcat is stopped.<br \/>\n<code><br \/>\n#! \/bin\/sh<br \/>\nSERVICE=\/etc\/init.d\/tomcat<br \/>\nSTOPPED_MESSAGE=\"Tomcat Servlet Container is not running.\"<\/p>\n<p>if [ \"`$SERVICE status`\" == \"$STOPPED_MESSAGE\"];<br \/>\nthen<br \/>\n{<br \/>\n  $SERVICE start<br \/>\n}<br \/>\nfi<br \/>\n<\/code><br \/>\nTo run the script every 10 minutes:<\/p>\n<li>\n1. Save the above script to \u201c\/root\/bin\/recover-tomcat.sh\u201d.<\/p>\n<p>2. Add execute permission:\n<\/li>\n<p><code><br \/>\nchmod +x \/root\/bin\/recover-tomcat.sh<\/code><\/p>\n<p>3. Add this to root\u2019s crontab, type the following as root:<br \/>\n<code><br \/>\ncrontab -e<\/code><br \/>\n4. Add the following lines to the crontab:<br \/>\n<code><br \/>\n# monitor tomcat every 10 minutes<br \/>\n*\/10 * * * * \/root\/bin\/recover-tomcat.sh<\/code><\/p>\n<p><strong>What if I don\u2019t have \/etc\/init.d\/tomcat* script on my computer?<\/strong><\/p>\n<p>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:<\/p>\n<p>if (the PID file does not exist)<br \/>\n<code><br \/>\n                                       {<br \/>\n  \/\/ conclude that tomcat is not running<br \/>\n  start tomcat<br \/>\n}<br \/>\nelse {<br \/>\n  read the process id from the PID file<br \/>\n  if (no process that id is running) {<br \/>\n    \/\/ conclude that tomcat has crashed<br \/>\n    start tomcat<br \/>\n  }<br \/>\n}<br \/>\n<\/code><br \/>\nYou 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.<br \/>\n<code><br \/>\n# adjust this to reflect tomcat home on your computer<br \/>\nTOMCAT_HOME=\/opt\/tomcat5<\/p>\n<p>if [ -f $TOMCAT_HOME\/bin\/tomcat.pid ]<br \/>\nthen<br \/>\n  echo \"PID file exists\"<br \/>\n  pid=\"`cat $TOMCAT_HOME\/bin\/tomcat.pid`\"<br \/>\n  if [ \"X`ps -p $pid | awk '{print $1}' | tail -1`\" = \"X\"]<br \/>\n  then<br \/>\n    echo \"Tomcat is running\"<br \/>\n  else<br \/>\n    echo \"Tomcat had crashed\"<br \/>\n    $TOMCAT_HOME\/bin\/startup.sh<br \/>\n  fi<br \/>\nelse<br \/>\n  echo \"PID file does not exist. Restarting...\"<br \/>\n  $TOMCAT_HOME\/bin\/startup.sh<br \/>\nfi<br \/>\n<\/code><\/p>\n<p><strong>Why would tomcat crash?<\/strong><\/p>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>From vineetmanohar.com 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\u2019s the pseudo logic: every few minutes {&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/g33kinfo.com\/info\/automatically-recover-tomcat-from-crashes\/\">Read More<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-3212","post","type-post","status-publish","format-standard","hentry","category-info"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Automatically recover Tomcat from crashes - Linux Shtuff<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/g33kinfo.com\/info\/automatically-recover-tomcat-from-crashes\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Automatically recover Tomcat from crashes - Linux Shtuff\" \/>\n<meta property=\"og:description\" content=\"From vineetmanohar.com 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\u2019s the pseudo logic: every few minutes {... Read More\" \/>\n<meta property=\"og:url\" content=\"https:\/\/g33kinfo.com\/info\/automatically-recover-tomcat-from-crashes\/\" \/>\n<meta property=\"og:site_name\" content=\"Linux Shtuff\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/fb.me\/g33kinf0\" \/>\n<meta property=\"article:author\" content=\"https:\/\/fb.me\/g33kinf0\" \/>\n<meta property=\"article:published_time\" content=\"2010-06-29T00:51:03+00:00\" \/>\n<meta name=\"author\" content=\"g33kadmin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/drsinger1111\" \/>\n<meta name=\"twitter:site\" content=\"@drsinger1111\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/automatically-recover-tomcat-from-crashes\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/automatically-recover-tomcat-from-crashes\\\/\"},\"author\":{\"name\":\"g33kadmin\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#\\\/schema\\\/person\\\/c022e4c40b13ea1b678e6f020756f547\"},\"headline\":\"Automatically recover Tomcat from crashes\",\"datePublished\":\"2010-06-29T00:51:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/automatically-recover-tomcat-from-crashes\\\/\"},\"wordCount\":325,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#\\\/schema\\\/person\\\/c022e4c40b13ea1b678e6f020756f547\"},\"articleSection\":[\"General Info\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/g33kinfo.com\\\/info\\\/automatically-recover-tomcat-from-crashes\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/automatically-recover-tomcat-from-crashes\\\/\",\"url\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/automatically-recover-tomcat-from-crashes\\\/\",\"name\":\"Automatically recover Tomcat from crashes - Linux Shtuff\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#website\"},\"datePublished\":\"2010-06-29T00:51:03+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/automatically-recover-tomcat-from-crashes\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/g33kinfo.com\\\/info\\\/automatically-recover-tomcat-from-crashes\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/automatically-recover-tomcat-from-crashes\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Automatically recover Tomcat from crashes\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#website\",\"url\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/\",\"name\":\"Linux Shtuff\",\"description\":\"Because I have CRS Syndrome...\",\"publisher\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#\\\/schema\\\/person\\\/c022e4c40b13ea1b678e6f020756f547\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#\\\/schema\\\/person\\\/c022e4c40b13ea1b678e6f020756f547\",\"name\":\"g33kadmin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/minion-researchA.gif\",\"url\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/minion-researchA.gif\",\"contentUrl\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/minion-researchA.gif\",\"width\":512,\"height\":512,\"caption\":\"g33kadmin\"},\"logo\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/minion-researchA.gif\"},\"description\":\"I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com\\\/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....\",\"sameAs\":[\"https:\\\/\\\/thelinuxreport.com\",\"https:\\\/\\\/fb.me\\\/g33kinf0\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/drsinger1111\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Automatically recover Tomcat from crashes - Linux Shtuff","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/g33kinfo.com\/info\/automatically-recover-tomcat-from-crashes\/","og_locale":"en_US","og_type":"article","og_title":"Automatically recover Tomcat from crashes - Linux Shtuff","og_description":"From vineetmanohar.com 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\u2019s the pseudo logic: every few minutes {... Read More","og_url":"https:\/\/g33kinfo.com\/info\/automatically-recover-tomcat-from-crashes\/","og_site_name":"Linux Shtuff","article_publisher":"https:\/\/fb.me\/g33kinf0","article_author":"https:\/\/fb.me\/g33kinf0","article_published_time":"2010-06-29T00:51:03+00:00","author":"g33kadmin","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/drsinger1111","twitter_site":"@drsinger1111","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/g33kinfo.com\/info\/automatically-recover-tomcat-from-crashes\/#article","isPartOf":{"@id":"https:\/\/g33kinfo.com\/info\/automatically-recover-tomcat-from-crashes\/"},"author":{"name":"g33kadmin","@id":"https:\/\/g33kinfo.com\/info\/#\/schema\/person\/c022e4c40b13ea1b678e6f020756f547"},"headline":"Automatically recover Tomcat from crashes","datePublished":"2010-06-29T00:51:03+00:00","mainEntityOfPage":{"@id":"https:\/\/g33kinfo.com\/info\/automatically-recover-tomcat-from-crashes\/"},"wordCount":325,"commentCount":0,"publisher":{"@id":"https:\/\/g33kinfo.com\/info\/#\/schema\/person\/c022e4c40b13ea1b678e6f020756f547"},"articleSection":["General Info"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/g33kinfo.com\/info\/automatically-recover-tomcat-from-crashes\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/g33kinfo.com\/info\/automatically-recover-tomcat-from-crashes\/","url":"https:\/\/g33kinfo.com\/info\/automatically-recover-tomcat-from-crashes\/","name":"Automatically recover Tomcat from crashes - Linux Shtuff","isPartOf":{"@id":"https:\/\/g33kinfo.com\/info\/#website"},"datePublished":"2010-06-29T00:51:03+00:00","breadcrumb":{"@id":"https:\/\/g33kinfo.com\/info\/automatically-recover-tomcat-from-crashes\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/g33kinfo.com\/info\/automatically-recover-tomcat-from-crashes\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/g33kinfo.com\/info\/automatically-recover-tomcat-from-crashes\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/g33kinfo.com\/info\/"},{"@type":"ListItem","position":2,"name":"Automatically recover Tomcat from crashes"}]},{"@type":"WebSite","@id":"https:\/\/g33kinfo.com\/info\/#website","url":"https:\/\/g33kinfo.com\/info\/","name":"Linux Shtuff","description":"Because I have CRS Syndrome...","publisher":{"@id":"https:\/\/g33kinfo.com\/info\/#\/schema\/person\/c022e4c40b13ea1b678e6f020756f547"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/g33kinfo.com\/info\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/g33kinfo.com\/info\/#\/schema\/person\/c022e4c40b13ea1b678e6f020756f547","name":"g33kadmin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/g33kinfo.com\/info\/wp-content\/uploads\/2022\/07\/minion-researchA.gif","url":"https:\/\/g33kinfo.com\/info\/wp-content\/uploads\/2022\/07\/minion-researchA.gif","contentUrl":"https:\/\/g33kinfo.com\/info\/wp-content\/uploads\/2022\/07\/minion-researchA.gif","width":512,"height":512,"caption":"g33kadmin"},"logo":{"@id":"https:\/\/g33kinfo.com\/info\/wp-content\/uploads\/2022\/07\/minion-researchA.gif"},"description":"I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com\/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....","sameAs":["https:\/\/thelinuxreport.com","https:\/\/fb.me\/g33kinf0","https:\/\/x.com\/https:\/\/twitter.com\/drsinger1111"]}]}},"_links":{"self":[{"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/posts\/3212","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/comments?post=3212"}],"version-history":[{"count":0,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/posts\/3212\/revisions"}],"wp:attachment":[{"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/media?parent=3212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/categories?post=3212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/tags?post=3212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}