Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.53 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. #stream-monitor.sh
  4. #Developed on Debian
  5. #Requires mpd
  6. #
  7. #Its an extension of http://mpd.wikia.com/wiki/Hack:stream-monitor?oldid=3932
  8. # You can use the same Init-Script
  9. #
  10. #To Install:
  11. # cp stream-monitor.sh /usr/bin/stream-monitor.sh
  12. # chmod 755 /usr/bin/stream-monitor.sh
  13. #
  14. #This script monitors mpd while it is playing an internet stream. It checks every $INTERVAL_CHECK seconds
  15. # to see if the status time variable has changed. (The stream is not playing when the time
  16. # is not changing.) Then it restart playing.
  17. #It also checks the current state of MPD and only take care of state 'play'. This provides you the comfort
  18. # to use MPD like before without have to care about the watchdog when you want to pause or stop the stream.
  19. #If the mpd daemon is not running for some reason, it starts it. i.e. if mpd were to crash.
  20. #
  21. #This is great for a robust stream player that keeps on playing, even after a network
  22. # outage or stream server outage. Useful in situations where the networking is less than perfect.
  23. #
  24. #Check with your streaming server provider before running this. It's not nice to connect to
  25. # a stream 24/7 if you don't need it or own it.
  26. #
  27. #Also does logging trough syslog.
  28. #
  29.  
  30. INTERVAL_CHECK=5  # Seconds between checks
  31. INTERVAL_WAIT=10  # Seconds to wait after mpd (re)start (buffering)
  32.  
  33. LOG_TAG="mpd-watchdog"
  34.  
  35. LOGGER="logger -st \"$LOG_TAG\" -- "
  36.  
  37.  
  38. $LOGGER Startup with interval of $INTERVAL_CHECK Seconds
  39.  
  40.  
  41. OLD_TIME=
  42. OLD_STATE="UNKNOWN"
  43.  
  44. while sleep $INTERVAL_CHECK;
  45. do
  46.     STATUS=`ps -A | grep -c mpd`
  47.         if [ $STATUS -eq 0 ]; then
  48.                 $LOGGER "no mpd processes, starting mpd"
  49.                 /etc/init.d/mpd start
  50.                 $LOGGER waiting $INTERVAL_WAIT Seconds...
  51.                 sleep $INTERVAL_WAIT
  52.                 continue
  53.         fi
  54.  
  55.  
  56.     TIME="0"
  57.     STATE="UNKNOWN"
  58.  
  59.     RESP=`echo -e "status\\nclose" | nc localhost 6600 `
  60.  
  61.     IFS=": "
  62.     while read KEY VALUE; do
  63.         case "$KEY" in
  64.             state) STATE="$VALUE";;
  65.             time)  TIME="$VALUE";;
  66.         esac
  67.     done <<<"$RESP"
  68.     unset IFS
  69.  
  70.     if   [ "$OLD_STATE" != "$STATE" ]; then
  71.         $LOGGER "MPD changed state from '$OLD_STATE' to '$STATE' "
  72.     fi
  73.  
  74.     if   [ "$STATE" == "play" ]; then
  75.  
  76.                 if [ "$TIME" = "$OLD_TIME" ]; then
  77.                         $LOGGER "mpd hanging, restarting"
  78.                         mpc stop
  79.                         mpc play
  80.                         $LOGGER waiting $INTERVAL_WAIT Seconds...
  81.             sleep $INTERVAL_WAIT
  82.                 fi
  83.         fi
  84.  
  85.         OLD_STATE=$STATE
  86.     OLD_TIME=$TIME
  87. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement