How to detect if Red5 services is running and restart if it had been down

Ngày 3 tháng 1 năm 2013 Trương Chương Dương
  1. Create checkred5running.sh in the red5 root folder
  2. Enter these code:
    #!/bin/sh
    #Servies name
    SERVICE='red5 (123.456.789.012)'
    # Path to RMS install folder (contains red5.sh)
    SERVER_PATH=/data/red5
    #Send report to email
    EMAILS=truong@chuongduong.net
    
    wget -S --server-response --spider -o checkred5server5080.log http://123.456.789.012:5080
    if grep "HTTP/1.1 200 OK" checkred5server5080.log > /dev/null
    then
       echo "$SERVICE service running, everything are fine"
       rm checkred5server5080.log
    else
       echo "$SERVICE is not running"
       rm checkred5server5080.log
       
       #send alert email
       echo "Red5 Appeared To Be Down ..." | mail -s "$SERVICE is down - FOUND PROBLEM" $EMAILS
    
       #Start RED5
       cd $SERVER_PATH
       sh red5.sh >/dev/null 2>&1 &
       
       counter=0
    
       while :
       do
          wget -S --server-response --spider -o checkred5server5080.log http://123.456.789.012:5080
          if grep "HTTP/1.1 200 OK" checkred5server5080.log > /dev/null
          then
             echo "Red5 had been started. Issue had been solved."
             echo "Red5 had been started. Issue had been solved." | mail -s "$SERVICE is running - SOLVED" $EMAILS
             rm checkred5server5080.log
             exit 0
          else
             echo "."
             counter=$(($counter+1))
             rm checkred5server5080.log
             #wating 2 sec
             sleep 2
          fi
    
          #Waiting around 1 minute, other checking script will execute soon
          if [ $counter -gt 28 ]
          then
             if ps aux | grep -v grep | grep root | grep java > /dev/null
             then
                #If services is still in process list, it cause other checking script meet error
                #We should keep this script continue running for checking
             else
                #Red5 servives had been killed again, we stop this script and other will try again
    
                echo "Red5 did not start yet."
                echo "Can not restart Red5 services. I will try again around 10 seconds later." | mail -s "$SERVICE is down - CAN NOT FIX - WILL TRY AGAIN SOON" $EMAILS
                exit 1
             fi
          fi
    
          if [ $counter -gt 60 ]
          then
             #Waiting for 2 minutes but red5 still did not start
             #I think we shoud start it by manual
    
             echo "Can not start Red5."
             echo "Can not restart Red5 services.\nRed5 process is now running on our system but I can not connect to media server. May it meet some problems when starting.\nPlease check it and restart by your self." | mail -s "$SERVICE is down - CAN NOT FIX - PLEASE CHECK IT BY YOUR SELF" $EMAILS
             exit 2
          fi
       done
    fi
    
  3. Replace 123.456.789.012 by the your server's IP or domain. Replace 5080 by the red5 HTTP port to match with your setting.
  4. Implement a cronjob to run above script in every minute. From now, the script will run every minutes to detect if Red5 is running or was down. If Red5 was down, it cannot get the content from red5 http webpage, so it will try to restart the red5 services and send to your email an alert.
Đang tải dữ liệu...