Fandom

Music Player Daemon Community Wiki

Hack:stream-monitor

525pages on
this wiki
Add New Page
Talk0 Share

stream-monitor.sh - monitors mpd while it is playing an internet stream.

#!/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/local/bin/stream-monitor.sh
# chmod 755 /usr/local/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


stream-monitor - Debian startup script to launch stream-monitor.sh

#! /bin/sh
set -e
#
#stream-monitor.sh launch script
#This script is a Debian startup script that handles the startup of stream-monitor.sh
#You may need to use something a little different on other OSs
#
#To Install on Debian:
# cp stream-monitor /etc/init.d/stream-monitor
# chmod 755 /etc/init.d/stream-monitor
# update-rc.d stream-monitor defaults 70

test -x /usr/local/bin/stream-monitor.sh || exit 0 #test for stream-monitor.sh in /usr/local/bin

case "$1" in
  start)
        echo -n "Starting stream-monitor"
        start-stop-daemon --start --make-pidfile --background --quiet --pidfile /var/run/stream-monitor.pid --exec /usr/local/bin/stream-monitor.sh
        echo "."
        ;;
  stop)
        echo -n "Stopping stream-monitor"
        start-stop-daemon --stop --pidfile /var/run/stream-monitor.pid
        echo "."
        ;;

  restart)
        echo -n "Restarting stream-monitor"
        start-stop-daemon --stop --pidfile /var/run/stream-monitor.pid
        start-stop-daemon --start --make-pidfile --background --quiet --pidfile /var/run/stream-monitor.pid --exec /usr/local/bin/stream-monitor.sh
        echo "."
        ;;

  *)
        echo "Usage: /etc/init.d/stream-monitor {start|stop|restart}"
        exit 1
esac

exit 0

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Also on Fandom

Random Wiki