[asterisk-dev] init script

John Cahill john at dmcip.com
Fri May 20 07:19:28 CDT 2011


Hi,

I've re-written the asterisk init script so that it supports the status option and returns LSB compliant values for the various scenarios defined in the LSB standard,( see http://refspecs.freestandards.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/iniscrptact.html). I did this so I could use it in a HA cluster I am currently building.

I've tested it with asterisk 1.6 and 1.8, on Debian Lenny and Squeeze, it appears to work fine.

If it's up to scratch, how do I go about contributing it to the Asterisk project?

Feedback appreciated.

Thanks.

John Cahill

------------------------------------------------------------------------------------------------------------------------------
#! /bin/sh
# $Id: rc.debian.asterisk 287198 2010-09-16 22:12:51Z qwell $
# Fri May 20 2011 John Cahill <email at johncahill.net>
# - Added support for status and ensured return values are LSB compliant so as this script can be used for Linux HA etc.
#
# Mon Jun 04 2007 Iñaki Baz Castillo <ibc at in.ilimit.es>
# - Eliminated SAFE_ASTERISK since it doesn't work as LSB script (it could require a independent "safe_asterisk" init script).s
# - Load and use the standar "/lib/lsb/init-functions".
# - Added "--oknodo" to "start-stop-daemon" for compatibility with LSB:
#   http://www.linux-foundation.org/spec/refspecs/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
#
# Thu Nov 17 2005 Gregory Boehnlein <damin at nacs.net>
# - Reversed behavior of LD_ASSUME_KERNEL=2.4.1
# - Added detailed failure messages
#
# Sun Jul 18 2004 Gregory Boehnlein <damin at nacs.net>
# - Added test for safe_asterisk
# - Changed "stop gracefully" to "stop now"
# - Added support for -U and -G command line options
# - Modified "reload" to call asterisk -rx 'reload'

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=asterisk
DESC="Asterisk PBX"
# Full path to asterisk binary
DAEMON=/usr/sbin/asterisk
ASTVARRUNDIR=/var/run/asterisk
ASTETCDIR=/etc/asterisk
TRUE=/bin/true

# Uncomment this ONLY if you know what you are doing.
# export LD_ASSUME_KERNEL=2.4.1

# Uncomment the following and set them to the user/groups that you
# want to run Asterisk as. NOTE: this requires substantial work to
# be sure that Asterisk's environment has permission to write the
# files required  for  its  operation, including logs, its comm
# socket, the asterisk database, etc.
#AST_USER=
#AST_GROUP=

# If you DON'T want Asterisk to start up with terminal colors, comment
# this out.
COLOR=yes

### BEGIN INIT INFO
# Provides:                asterisk
# Required-Start:    $network $syslog $named $local_fs $remote_fs
# Required-Stop:     $network $syslog $named $local_fs $remote_fs
# Should-Start:      dahdi misdn lcr wanrouter mysql postgresql
# Should-Stop:       dahdi misdn lcr wanrouter mysql postgresql
# Default-Start:        2 3 4 5
# Default-Stop:                0 1 6
# Short-Description:        Asterisk PBX
# Description:                the Asterisk Open Source PBX
### END INIT INFO


if ! [ -x $DAEMON ] ; then
        echo "ERROR: $DAEMON not found"
        echo "result: 0"
        exit 0
fi

if ! [ -d $ASTETCDIR ] ; then
        echo "ERROR: $ASTETCDIR directory not found"
        echo "result: 0"
        exit 0
fi

# Use the LSB standard functions for services management
. /lib/lsb/init-functions

case "$1" in
  start)
        # Check if Asterisk is already running.  If it is, then bug out, because
        # starting up Asterisk when Asterisk is already running is very bad.
        VERSION=`${DAEMON} -rx 'core show version' || ${TRUE}`
        if [ -n "$(echo $VERSION | grep "Asterisk")" ]; then
                echo "Asterisk is already running.  $0 will exit now."
                echo "result: 0"
                exit 0
        fi

        log_begin_msg "Starting $DESC: $NAME"
        if [ ! -d $ASTVARRUNDIR ]; then
                mkdir -p $ASTVARRUNDIR
        fi
        if [ $AST_USER ] ; then
                ASTARGS="-U $AST_USER"
                chown $AST_USER $ASTVARRUNDIR
        fi
        if [ $AST_GROUP ] ; then
                ASTARGS="$ASTARGS -G $AST_GROUP"
                chgrp $AST_GROUP $ASTVARRUNDIR
        fi
        if test "x$COLOR" = "xno" ; then
                ASTARGS="$ASTARGS -n"
        fi
        # "start-stop-daemon --oknodo" returns 0 even if Asterisk was already running (as LSB expects):
        start-stop-daemon --start --oknodo --exec $DAEMON -- $ASTARGS
        log_end_msg $?
        ;;
  stop)
        log_begin_msg "Stopping $DESC: $NAME"
        # "start-stop-daemon --oknodo" returns 0 even if Asterisk was already stopped (as LSB expects):
        start-stop-daemon --stop --oknodo --exec $DAEMON
        log_end_msg $?
        ;;
  reload)
        echo "Reloading $DESC configuration files."
        $DAEMON -rx 'module reload' > /dev/null 2> /dev/null
        ;;
  restart|force-reload)
        $0 stop
        sleep 3  # It needs some time to really be stopped.
        $0 start
        # "restart|force-reload" starts Asterisk and returns 0 even if Asterisk was stopped (as LSB expects).
        ;;
  status)
        pidof -o %PPID $DAEMON 1>>/dev/null
        running_flag=$?



        echo "Checking Status $DESC: $NAME"
        if [ -f $ASTVARRUNDIR/asterisk.pid ]; then
                # pid file does exist: 0
                pid_file_exists=0
        else
                # pid file does not exist: 1
                pid_file_exists=1
        fi

        if [ -e $ASTVARRUNDIR/asterisk.ctl ]; then
                # asterisk.ctl exists
                asterisk_ctl_exists=0
        else
                asterisk_ctl_exists=1
        fi
        VERSION=`${DAEMON} -rx 'core show version' || ${TRUE}`
        if [ -n "$(echo $VERSION | grep "Asterisk")" ]; then
                echo "Asterisk is already running.  $0 will exit now."
                echo "result: 0"
                program_responsive=0
                exit 0
        else
                program_responsive=1
        fi



        # STATUS=0
        if [ $running_flag -eq 0 ] && [ $pid_file_exists -eq 0 ] && [ $asterisk_ctl_exists -eq 0 ] && [ $program_responsive -eq 0 ]; then
                echo "Program is running normally."
                echo "result: 0"
                program_running_normally=0
                exit 0
        else
                program_running_normally=1
        fi
#echo "running_flag: $running_flag,pid_file_exists: $pid_file_exists,asterisk_ctl_exists: $asterisk_ctl_exists,program_responsive: $program_responsive,program_running_normally: $program_running_normally"


        if [ $program_running_normally -eq 1 ]; then
                # STATUS=1
                if [ $running_flag -eq 1 ] && [ $pid_file_exists -eq 0 ]; then
                        echo "Program is dead but pid file exists."
                        echo "result: 1"
                        set -e
                        exit 1
                fi

                # STATUS=2 Not implemented - no lock file :-(

                # STATUS=3
                if [ $running_flag -eq 1 ] && [ $pid_file_exists -eq 1 ] && [ $asterisk_ctl_exists -eq 1 ]; then
                        echo "Program is not running."
                        echo "result: 3"
                        set -e
                        exit 3
                fi
                # STATUS=4 Program or service state unknown - used as crude catchall for values other than 0,1,3
                        echo "Program status is unknown."
                        echo "result: 4"
                        set -e
                        exit 4
        fi
        ;;
  *)
        echo "Usage: \etc\init.d\$NAME {start|stop|restart|reload|force-reload|status}" >&2
        echo "result: 2"
        set -e
        exit 2
        ;;
esac

------------------------------------------------------------------------------------------------------------------------




More information about the asterisk-dev mailing list