Ubuntu – Icecast2 Startup Script with Ices2 Support

Recently I set up an Icecast server to be able to stream music on hold to my Asterisk servers at work. Here is an updated init script for Icecast2 that adds support to start the Ices2 source client at the same time. For streaming audio from a playlist with Ogg support, Ices2 is a good candidate. For streaming audio from an mp3 based playlist, have a look at Ices0 that can be downloaded on the same site. There is also an array of different Icecast source clients to choose from here. This just happened to be the first one that I tried out. I ended up bastardizing the code somewhat to make it conform to my own personal preferences by using functions and what not to make it all pretty like. I also chose to run Ices2 as the same user as the icecast user, so you would have to update any permissions on the Ices2 directories and config files as needed.

#!/bin/sh
### BEGIN INIT INFO
# Provides:          icecast2
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts the icecast audio streaming server daemon
### END INIT INFO
#
# icecast2
#
#		Written by Miquel van Smoorenburg <miquels@cistron.nl>.
#		Modified for Debian
#		by Ian Murdock <imurdock@gnu.ai.mit.edu>.
#
#		Further modified by Keegan Quinn <ice@thebasement.org>
#		for use with Icecast 2
#
#		Violated by Nathan Thomas <nthomas@paperstreetoline.com>
#		Added support for ices2 source client and various other code tweaks
#

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/icecast2
NAME=icecast2
DESC="icecast2 streaming media server"
ICES_DAEMON=/usr/bin/ices2
ICES_NAME=ices2
ICES_DESC="ices2 ogg playlist plugin"

if [ ! -x $DAEMON ]; then
        echo "Cannot locate the $NAME executable. Exit"
        exit 1
elif [ ! -x $ICES_DAEMON ]; then
        echo "Cannot locate the $ICES_NAME executable. Exit"
        exit 1
fi

. /lib/lsb/init-functions

# Defaults
CONFIGFILE="/etc/icecast2/icecast.xml"
CONFIGDEFAULTFILE="/etc/default/icecast2"
USERID=icecast2
GROUPID=icecast
ENABLE="false"
ICES_CONFIGFILE="/etc/ices2/ices-playlist.xml"

# Reads config file (will override defaults above)
[ -r "$CONFIGDEFAULTFILE" ] && . $CONFIGDEFAULTFILE

if [ "$ENABLE" != "true" ]; then
        echo "$NAME daemon disabled - read $CONFIGDEFAULTFILE."
        exit 0
fi

set -e

start_icecast(){
        echo -n "Starting $DESC: "
        if [ -z `pidof $NAME` ] ; then
                start-stop-daemon --start --quiet --chuid $USERID:$GROUPID --exec $DAEMON -- -b -c $CONFIGFILE > /dev/null 2>&1
                if [ "$?" -eq 0 ] ; then
                        echo "OK"
                        return 0
                fi
        else
                echo "Failed...$NAME (pid `pidof $NAME`) is already running."
                return 1
        fi
}

stop_icecast(){
        echo -n "Stopping $DESC: "
        # Send TERM after 5 seconds, wait at most 30 seconds.
        start-stop-daemon --stop --oknodo --retry TERM/5/0/30 --quiet --name $NAME --exec $DAEMON > /dev/null 2>&1
        if [ -z `pidof $NAME` ] ; then
                echo "OK"
                return 0
        else
                echo "Failed...Couldn't kill $NAME process for some reason."
                return 1
        fi

}

status_icecast(){
        if [ -z `pidof $NAME` ] ; then
                echo "Service $NAME is: Stopped"
        else
                echo "Service $NAME is: Running...(pid `pidof $NAME`)"
        fi
}

start_ices(){
        echo -n "Starting $ICES_DESC: "
        if [ -z `pidof $ICES_NAME` ] ; then
                start-stop-daemon --start --quiet --chuid $USERID:$GROUPID --exec $ICES_DAEMON -- $ICES_CONFIGFILE > /dev/null 2>&1 # add an "&" at the end if background!=0 in ices2 xml file
                if [ "$?" -eq 0 ] ; then
                        echo "OK"
                        return 0
                fi
        else
                echo "Failed...$ICES_NAME (pid `pidof $ICES_NAME`) is already running."
                return 1
        fi

}

stop_ices(){
        echo -n "Stopping $ICES_DESC: "
        # Send TERM after 5 seconds, wait at most 30 seconds.
        start-stop-daemon --stop --oknodo --retry TERM/5/0/30 --quiet --name $ICES_NAME --exec $ICES_DAEMON > /dev/null 2>&1
        if [ -z `pidof $ICES_NAME` ] ; then
                echo "OK"
                return 0
        else
                echo "Failed...Couldn't kill $ICES_NAME process for some reason."
                return 1
        fi
}

status_ices(){
        if [ -z `pidof $ICES_NAME` ] ; then
                echo "Service $ICES_NAME is: Stopped"
        else
                echo "Service $ICES_NAME is: Running...(pid `pidof $ICES_NAME`)"
        fi
}

restart(){
        stop_ices
        stop_icecast
        start_icecast
        start_ices
}

case "$1" in
  start)
        start_icecast
        start_ices
        ;;
  stop)
        stop_ices
        stop_icecast
        ;;
  reload|force-reload)
        restart
        ;;
  restart)
        restart
        ;;
  status)
        status_icecast
        status_ices
        ;;
  *)
        echo "Usage: $0 {start|stop|restart|status|reload|force-reload}" >&2
        exit 1
        ;;
esac

exit 0

Leave a Reply