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 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.
The script checks if the backend service is running and if it isn’t then the script assumes you have stopped it on purpose and thus it doesn’t do anything more. If the service is running it then runs “wget” 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.
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.
This script uses “mailx” to send the email direct to your ISPs SMTP server for relaying to the appropriate email address.
Hopefully the rest of the script is intuitive enough and has enough in-line docs to let you figure out the rest.
check_mythtv_and_restart.sh
check_mythtv_and_restart.sh_.zip
#!/bin/bash # # Check MythTV backend is running and responding # ============================================== # # This script checks if MythBackend is running but not responding # and if so it then restarts the backend. # # This script has been tested with Fedora 16 and MythTV 0.25 # # Mythbackend deadlocks and auto-restart script # http://tweakingmythtv.wordpress.com/2011/01/11/mythbackend-deadlocks-and-auto-restart-script/ # # This script assumes that you need to authenticate against the SMTP mail # server and you don't need to use "starttls" # - If you mail server is different then you may have to change the script # at the points where it sends emails. # # Also this script emails once a day to indicate if Myth is ok # it reads a text file "TmpDailyEmailFlagFile". This file contains a date # It compares the date in the file with the current date, if they are different # it sends an email. Finally it writes the current date to the text file. # # # Settings # MythServiceName="mythbackend.service" MythHostName="localhost" MythTestPort="6544" MythTestTimeout="20" MailSvrHostName="mail.myisp.com" MailSvrPort="587" EmailToAddress="joe.bloggs@somedomain.com" EmailFromAddress="joe.bloggs@somedomain.com" EmailFromName="Joe Bloggs" TmpEmailBodyFileName="/tmp/tmp_mythbackend_email_body.txt" TmpDailyEmailFlagFile="/tmp/tmp_mythbackend_daily_flagfile.txt" SMTPUser="myispusername" SMTPPassword="supersecretpassword" # # Misc stuff # CurrDate="" CurrDate=`date +"%A %d/%m/%Y %I:%M%p"` CurrDay=`date +"%A %d/%m/%Y"` touch TmpDailyEmailFlagFile PrevFlagFileDate=`cat ${TmpDailyEmailFlagFile}` echo "PrevFlagFileDate = ${PrevFlagFileDate}" # # Check if MythBackend is actually running # TmpStatus="" TmpStatus=`systemctl status ${MythServiceName} | grep "active (running)"` if test "${TmpStatus}" != "" then MythServiceStatus="RUNNING" else MythServiceStatus="STOPPED" fi echo "DBUG Point A" echo "wget --spider -S \"http://${MythHostName}:${MythTestPort}\" --timeout=${MythTestTimeout} 2>&1 | grep \"HTTP/1.0 200 OK\"" TmpResponse="" TmpResponse=`wget --spider -S "http://${MythHostName}:${MythTestPort}" --timeout=${MythTestTimeout} 2>&1 | grep "HTTP/1.0 200 OK"` if test "${TmpResponse}" != "" then MythReplyStatus="OK" else MythReplyStatus="DOWN" fi echo "DBUG Point B" # # If MythBackend service is running but not responding restart # and email # echo "Service = ${MythServiceStatus}" echo "Response = ${MythReplyStatus}" if [[ "${MythServiceStatus}" == "RUNNING" ]] && [[ "${MythReplyStatus}" == "DOWN" ]] then # # If Myth Backend is both running and not responding then restart it # # Note we if the service is running we will assume it is off on purpose # echo echo "MythTV backend service is running but not responding." echo "restarting backend service." echo # # Restart Myth Backend # echo "Attempting restart of MythBackend" killall -9 mythbackend systemctl start mythbackend.service echo "Finished restart" # # Test again # TmpResponse="" TmpResponse=`wget --spider -S "http://${MythHostName}:${MythTestPort}" --timeout=${MythTestTimeout} 2>&1 | grep "HTTP/1.0 200 OK"` if test "${TmpResponse}" != "" then MythReplyStatusAfter="OK" else MythReplyStatusAfter="DOWN" fi TmpStatus="" TmpStatus=`systemctl status ${MythServiceName} | grep "active (running)"` if test "${TmpStatus}" != "" then MythServiceStatusAfter="RUNNING" else MythServiceStatusAfter="STOPPED" fi # # Send an email # EmailSubject="MythTV backend restarted ${CurrDate}" echo "hi," > ${TmpEmailBodyFileName} echo "" >> ${TmpEmailBodyFileName} echo "MythTV backend service had to restart." >> ${TmpEmailBodyFileName} echo "" >> ${TmpEmailBodyFileName} echo "The service status was ${MythServiceStatus}" >> ${TmpEmailBodyFileName} echo "The reply status was ${MythReplyStatus}" >> ${TmpEmailBodyFileName} echo "" >> ${TmpEmailBodyFileName} echo "The service status is now ${MythServiceStatusAfter}" >> ${TmpEmailBodyFileName} echo "The reply status is now ${MythReplyStatusAfter}" >> ${TmpEmailBodyFileName} echo "" >> ${TmpEmailBodyFileName} echo "Yours ${EmailFromName}" >> ${TmpEmailBodyFileName} export smtp="${MailSvrHostName}:${MailSvrPort}" cat ${TmpEmailBodyFileName} | mailx -s "${EmailSubject}" -S smtp-auth=login -S from=${EmailFromAddress} -S smtp-auth-user=${SMTPUser} -S smtp-auth-password=${SMTPPassword} "${EmailToAddress}" else if [[ "${PrevFlagFileDate}" != "${CurrDay}" ]] then # # If Myth Backend is OK then once a day send a status email - just once a day. # echo "Status OK email sent" echo "${CurrDay}" > ${TmpDailyEmailFlagFile} # # Send an email # EmailSubject="MythTV backend update ${CurrDate}. Service ${MythServiceStatus}, Avalilable ${MythReplyStatus}" echo "hi," > ${TmpEmailBodyFileName} echo "" >> ${TmpEmailBodyFileName} echo "Nothing needed to be done to Myth backend." >> ${TmpEmailBodyFileName} echo "" >> ${TmpEmailBodyFileName} echo "The service status is ${MythServiceStatus}" >> ${TmpEmailBodyFileName} echo "The reply status is ${MythReplyStatus}" >> ${TmpEmailBodyFileName} echo "" >> ${TmpEmailBodyFileName} echo "Yours ${EmailFromName}" >> ${TmpEmailBodyFileName} export smtp="${MailSvrHostName}:${MailSvrPort}" cat ${TmpEmailBodyFileName} | mailx -s "${EmailSubject}" -S smtp-auth=login -S from=${EmailFromAddress} -S smtp-auth-user=${SMTPUser} -S smtp-auth-password=${SMTPPassword} "${EmailToAddress}" fi fi
If you are looking at how to run a scheduled task or CRON job look at my post: