{"id":1751,"date":"2013-11-14T22:22:32","date_gmt":"2013-11-14T09:22:32","guid":{"rendered":"http:\/\/www.zoyinc.com\/?p=1751"},"modified":"2013-11-27T21:54:31","modified_gmt":"2013-11-27T08:54:31","slug":"cron-job-to-restart-mythbackend","status":"publish","type":"post","link":"http:\/\/www.zoyinc.com\/?p=1751","title":{"rendered":"Cron job to check and if needed restart MythBackend"},"content":{"rendered":"<p>I am running MythTV 0.25 on Fedora 16. The problem I have is that sometimes the backend stops responding for reasons I have not figured out. Until I resolve the problem I want a mechanism that will restart the backend when it stops responding.<\/p>\n<p>The solution I took was to create shell script that would check the backed to see if the service was started and whether it was responding. I have a Cron job that runs every 5 minutes and runs the below script.<\/p>\n<p>The script checks if the backend service is running and if it isn&#8217;t then the script assumes you have stopped it on purpose and thus it doesn&#8217;t do anything more. If the service is running it then runs &#8220;wget&#8221; to see if the backend is actually responding. Thus if the service is running but not responding it kills the process and restarts the service.<\/p>\n<p>This script also sends out an email each time it strikes a problem. It also sends an email at the start of every day just saying it has checked and Myth Backend is working.<\/p>\n<p>This script uses &#8220;mailx&#8221; to send the email direct to your ISPs SMTP server for relaying to the appropriate email address.<\/p>\n<p>Hopefully the rest of the script is intuitive enough and has enough in-line docs to let you figure out the rest.<\/p>\n<p>check_mythtv_and_restart.sh<br \/>\n<a  href=\"http:\/\/www.zoyinc.com\/wp-content\/uploads\/2013\/11\/check_mythtv_and_restart.sh_.zip\">check_mythtv_and_restart.sh_.zip<\/a><\/p>\n<pre class=\"lang:sh decode:true\">#!\/bin\/bash\r\n#\r\n# Check MythTV backend is running and responding\r\n# ==============================================\r\n# \r\n# This script checks if MythBackend is running but not responding\r\n# and if so it then restarts the backend.\r\n#\r\n# This script has been tested with Fedora 16 and MythTV 0.25\r\n#\r\n# Mythbackend deadlocks and auto-restart script\r\n# http:\/\/tweakingmythtv.wordpress.com\/2011\/01\/11\/mythbackend-deadlocks-and-auto-restart-script\/\r\n#\r\n# This script assumes that you need to authenticate against the SMTP mail\r\n# server and you don't need to use \"starttls\"\r\n# - If you mail server is different then you may have to change the script\r\n#   at the points where it sends emails.\r\n#\r\n# Also this script emails once a day to indicate if Myth is ok\r\n# it reads a text file \"TmpDailyEmailFlagFile\". This file contains a date\r\n# It compares the date in the file with the current date, if they are different\r\n# it sends an email. Finally it writes the current date to the text file.\r\n#\r\n\r\n#\r\n# Settings\r\n#\r\nMythServiceName=\"mythbackend.service\"\r\nMythHostName=\"localhost\"\r\nMythTestPort=\"6544\"\r\nMythTestTimeout=\"20\"\r\nMailSvrHostName=\"mail.myisp.com\"\r\nMailSvrPort=\"587\"\r\nEmailToAddress=\"joe.bloggs@somedomain.com\"\r\nEmailFromAddress=\"joe.bloggs@somedomain.com\"\r\nEmailFromName=\"Joe Bloggs\"\r\nTmpEmailBodyFileName=\"\/tmp\/tmp_mythbackend_email_body.txt\"\r\nTmpDailyEmailFlagFile=\"\/tmp\/tmp_mythbackend_daily_flagfile.txt\"\r\nSMTPUser=\"myispusername\"\r\nSMTPPassword=\"supersecretpassword\"\r\n\r\n#\r\n# Misc stuff\r\n#\r\nCurrDate=\"\"\r\nCurrDate=`date +\"%A %d\/%m\/%Y %I:%M%p\"`\r\nCurrDay=`date +\"%A %d\/%m\/%Y\"`\r\n\r\ntouch TmpDailyEmailFlagFile\r\nPrevFlagFileDate=`cat ${TmpDailyEmailFlagFile}`\r\necho \"PrevFlagFileDate = ${PrevFlagFileDate}\"\r\n\r\n#\r\n# Check if MythBackend is actually running\r\n#\r\nTmpStatus=\"\"\r\nTmpStatus=`systemctl status ${MythServiceName} | grep \"active (running)\"`\r\nif \r\n   test \"${TmpStatus}\" != \"\"\r\nthen\r\n   MythServiceStatus=\"RUNNING\"\r\nelse\r\n   MythServiceStatus=\"STOPPED\"\r\nfi\r\necho \"DBUG Point A\"\r\necho \"wget --spider -S \\\"http:\/\/${MythHostName}:${MythTestPort}\\\" --timeout=${MythTestTimeout}  2&gt;&amp;1 | grep \\\"HTTP\/1.0 200 OK\\\"\"\r\nTmpResponse=\"\"\r\nTmpResponse=`wget --spider -S \"http:\/\/${MythHostName}:${MythTestPort}\" --timeout=${MythTestTimeout}  2&gt;&amp;1 | grep \"HTTP\/1.0 200 OK\"`\r\nif \r\n   test \"${TmpResponse}\" != \"\"\r\nthen\r\n   MythReplyStatus=\"OK\"\r\nelse\r\n   MythReplyStatus=\"DOWN\"\r\nfi\r\necho \"DBUG Point B\"\r\n\r\n#\r\n# If MythBackend service is running but not responding restart \r\n# and email\r\n#\r\necho \"Service = ${MythServiceStatus}\"\r\necho \"Response = ${MythReplyStatus}\"\r\nif [[ \"${MythServiceStatus}\" == \"RUNNING\" ]] &amp;&amp;  [[ \"${MythReplyStatus}\" == \"DOWN\" ]]\r\nthen\r\n\r\n   #\r\n   # If Myth Backend is both running and not responding then restart it\r\n   #\r\n   # Note we if the service is running we will assume it is off on purpose\r\n   # \r\n   echo\r\n   echo \"MythTV backend service is running but not responding.\"\r\n   echo \"restarting backend service.\"\r\n   echo\r\n\r\n   #\r\n   # Restart Myth Backend\r\n   #\r\n   echo \"Attempting restart of MythBackend\"\r\n   killall -9 mythbackend\r\n   systemctl start mythbackend.service\r\n   echo \"Finished restart\"\r\n\r\n   #\r\n   # Test again\r\n   # \r\n   TmpResponse=\"\"\r\n   TmpResponse=`wget --spider -S \"http:\/\/${MythHostName}:${MythTestPort}\" --timeout=${MythTestTimeout}  2&gt;&amp;1 | grep \"HTTP\/1.0 200 OK\"`\r\n   if \r\n      test \"${TmpResponse}\" != \"\"\r\n   then\r\n      MythReplyStatusAfter=\"OK\"\r\n   else\r\n      MythReplyStatusAfter=\"DOWN\"\r\n   fi\r\n   TmpStatus=\"\"\r\n   TmpStatus=`systemctl status ${MythServiceName} | grep \"active (running)\"`\r\n   if \r\n      test \"${TmpStatus}\" != \"\"\r\n   then\r\n      MythServiceStatusAfter=\"RUNNING\"\r\n   else\r\n      MythServiceStatusAfter=\"STOPPED\"\r\n   fi\r\n\r\n   #\r\n   # Send an email\r\n   #\r\n   EmailSubject=\"MythTV backend restarted ${CurrDate}\"\r\n   echo \"hi,\" &gt; ${TmpEmailBodyFileName}\r\n   echo \"\" &gt;&gt; ${TmpEmailBodyFileName}\r\n   echo \"MythTV backend service had to restart.\" &gt;&gt; ${TmpEmailBodyFileName}\r\n   echo \"\" &gt;&gt; ${TmpEmailBodyFileName}\r\n   echo \"The service status was  ${MythServiceStatus}\" &gt;&gt; ${TmpEmailBodyFileName}\r\n   echo \"The reply status was ${MythReplyStatus}\" &gt;&gt; ${TmpEmailBodyFileName}\r\n   echo \"\" &gt;&gt; ${TmpEmailBodyFileName}\r\n   echo \"The service status is now  ${MythServiceStatusAfter}\" &gt;&gt; ${TmpEmailBodyFileName}\r\n   echo \"The reply status is now ${MythReplyStatusAfter}\" &gt;&gt; ${TmpEmailBodyFileName}\r\n   echo \"\" &gt;&gt; ${TmpEmailBodyFileName}\r\n   echo \"Yours ${EmailFromName}\" &gt;&gt; ${TmpEmailBodyFileName}\r\n\r\n   export smtp=\"${MailSvrHostName}:${MailSvrPort}\"\r\n   cat ${TmpEmailBodyFileName} | mailx -s \"${EmailSubject}\" -S smtp-auth=login -S from=${EmailFromAddress} -S smtp-auth-user=${SMTPUser} -S smtp-auth-password=${SMTPPassword} \"${EmailToAddress}\"\r\nelse\r\n   if [[ \"${PrevFlagFileDate}\" != \"${CurrDay}\" ]]\r\n   then\r\n\r\n      #\r\n      # If Myth Backend is OK then once a day send a status email - just once a day.\r\n      #\r\n      echo \"Status OK email sent\"\r\n      echo \"${CurrDay}\" &gt; ${TmpDailyEmailFlagFile}\r\n\r\n      #\r\n      # Send an email\r\n      #\r\n      EmailSubject=\"MythTV backend update ${CurrDate}. Service ${MythServiceStatus},  Avalilable ${MythReplyStatus}\"\r\n      echo \"hi,\" &gt; ${TmpEmailBodyFileName}\r\n      echo \"\" &gt;&gt; ${TmpEmailBodyFileName}\r\n      echo \"Nothing needed to be done to Myth backend.\" &gt;&gt; ${TmpEmailBodyFileName}\r\n      echo \"\" &gt;&gt; ${TmpEmailBodyFileName}\r\n      echo \"The service status is ${MythServiceStatus}\" &gt;&gt; ${TmpEmailBodyFileName}\r\n      echo \"The reply status is ${MythReplyStatus}\" &gt;&gt; ${TmpEmailBodyFileName}\r\n      echo \"\" &gt;&gt; ${TmpEmailBodyFileName}\r\n      echo \"Yours ${EmailFromName}\" &gt;&gt; ${TmpEmailBodyFileName}\r\n\r\n      export smtp=\"${MailSvrHostName}:${MailSvrPort}\"\r\n      cat ${TmpEmailBodyFileName} | mailx -s \"${EmailSubject}\" -S smtp-auth=login -S from=${EmailFromAddress} -S smtp-auth-user=${SMTPUser} -S smtp-auth-password=${SMTPPassword} \"${EmailToAddress}\"\r\n\r\n   fi\r\nfi<\/pre>\n<p>If you are looking at how to run a scheduled task or CRON job look at my post:<\/p>\n<p style=\"padding-left: 30px;\"><a  title=\"Gnome Schedule \u2013 a GUI to CRON or AT\" href=\"http:\/\/www.zoyinc.com\/?p=1808\">Gnome Schedule &#8211; a GUI to CRON or AT<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am running MythTV 0.25 on Fedora 16. The problem I have is that sometimes the backend stops responding for reasons I have not figured out. Until I resolve the problem I want a mechanism that will restart the backend when it stops responding. The solution I took was to create shell script that would [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":873,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,18,206],"tags":[207,209,296,208,298,211,210],"class_list":["post-1751","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-fedora","category-mythtv","category-shell-scripts","tag-backend","tag-cron","tag-fedora","tag-media","tag-mythtv","tag-script","tag-shell"],"_links":{"self":[{"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/posts\/1751","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1751"}],"version-history":[{"count":9,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/posts\/1751\/revisions"}],"predecessor-version":[{"id":1820,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/posts\/1751\/revisions\/1820"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/media\/873"}],"wp:attachment":[{"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1751"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}