Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- #stream-monitor.sh
- #Developed on Debian
- #Requires mpd
- #
- #Its an extension of http://mpd.wikia.com/wiki/Hack:stream-monitor?oldid=3932
- # You can use the same Init-Script
- #
- #To Install:
- # cp stream-monitor.sh /usr/bin/stream-monitor.sh
- # chmod 755 /usr/bin/stream-monitor.sh
- #
- #This script monitors mpd while it is playing an internet stream. It checks every $INTERVAL_CHECK seconds
- # to see if the status time variable has changed. (The stream is not playing when the time
- # is not changing.) Then it restart playing.
- #It also checks the current state of MPD and only take care of state 'play'. This provides you the comfort
- # to use MPD like before without have to care about the watchdog when you want to pause or stop the stream.
- #If the mpd daemon is not running for some reason, it starts it. i.e. if mpd were to crash.
- #
- #This is great for a robust stream player that keeps on playing, even after a network
- # outage or stream server outage. Useful in situations where the networking is less than perfect.
- #
- #Check with your streaming server provider before running this. It's not nice to connect to
- # a stream 24/7 if you don't need it or own it.
- #
- #Also does logging trough syslog.
- #
- INTERVAL_CHECK=5 # Seconds between checks
- INTERVAL_WAIT=10 # Seconds to wait after mpd (re)start (buffering)
- LOG_TAG="mpd-watchdog"
- LOGGER="logger -st \"$LOG_TAG\" -- "
- $LOGGER Startup with interval of $INTERVAL_CHECK Seconds
- OLD_TIME=
- OLD_STATE="UNKNOWN"
- while sleep $INTERVAL_CHECK;
- do
- STATUS=`ps -A | grep -c mpd`
- if [ $STATUS -eq 0 ]; then
- $LOGGER "no mpd processes, starting mpd"
- /etc/init.d/mpd start
- $LOGGER waiting $INTERVAL_WAIT Seconds...
- sleep $INTERVAL_WAIT
- continue
- fi
- TIME="0"
- STATE="UNKNOWN"
- RESP=`echo -e "status\\nclose" | nc localhost 6600 `
- IFS=": "
- while read KEY VALUE; do
- case "$KEY" in
- state) STATE="$VALUE";;
- time) TIME="$VALUE";;
- esac
- done <<<"$RESP"
- unset IFS
- if [ "$OLD_STATE" != "$STATE" ]; then
- $LOGGER "MPD changed state from '$OLD_STATE' to '$STATE' "
- fi
- if [ "$STATE" == "play" ]; then
- if [ "$TIME" = "$OLD_TIME" ]; then
- $LOGGER "mpd hanging, restarting"
- mpc stop
- mpc play
- $LOGGER waiting $INTERVAL_WAIT Seconds...
- sleep $INTERVAL_WAIT
- fi
- fi
- OLD_STATE=$STATE
- OLD_TIME=$TIME
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement