summaryrefslogtreecommitdiff
path: root/asterisk/asterisk.rc
blob: 7034937914971eb826190b19bbce8c0091315df7 (plain)
    1 #!/bin/bash
    2 
    3 NAME=asterisk
    4 USER=asterisk
    5 GROUP=asterisk
    6 RUNDIR=/var/run/$NAME
    7 PIDFILE=$RUNDIR/$NAME.pid
    8 STARTCMD="(cd /; /usr/sbin/asterisk -G $GROUP -U $USER)"
    9 STOPCMD="/usr/sbin/asterisk -r -x 'core stop now'"
   10 STOPGRACECMD="/usr/sbin/asterisk -r -x 'core stop gracefully'"
   11 STOPTIMEOUT=300
   12 
   13 function getpid() {
   14 	if [ -z "$PIDFILE" ]; then
   15 		pid="$(pgrep -xfn "$STARTCMD")"
   16 	else
   17 		if [ -f "$PIDFILE" ]; then
   18 			pid=$(< $PIDFILE)
   19 			if [ ! -d /proc/"$pid" ]; then
   20 				echo "$NAME: removing stale pidfile $PIDFILE" >&2
   21 				rm -f "$PIDFILE"
   22 				unset pid
   23 			fi
   24 		fi
   25 	fi
   26 	echo "$pid"
   27 }
   28 
   29 case $1 in
   30 start)
   31 	pid=$(getpid)
   32 	install -d -m 755 -o $USER $RUNDIR || exit 1
   33 	if [ -n "$pid" ]; then
   34 		echo "$NAME already running with pid $pid" >&2
   35 		exit 1
   36 	fi
   37 	eval "$STARTCMD"
   38 	;;
   39 stop|stopnice)
   40 	pid=$(getpid)
   41 	if [ -n "$pid" ]; then
   42 		if [ "$1" == "stop" ]; then
   43 			if [ -n "$STOPCMD" ]; then
   44 				eval "$STOPCMD"
   45 			else
   46 				kill "$pid"
   47 			fi
   48 		else
   49 			if [ -n "$STOPCMD" ]; then
   50 				eval "$STOPGRACECMD"
   51 			else
   52 				echo "$NAME: $1 not implemented"
   53 				exit 1
   54 			fi
   55 		fi
   56 		t=$(printf '%(%s)T' -1)
   57 		tend=$((t+STOPTIMEOUT))
   58 		while [ -d /proc/$pid -a $t -lt $tend ]; do
   59 			sleep 0.5
   60 			t=$(printf '%(%s)T' -1)
   61 		done
   62 		if [ -d /proc/"$pid" ]; then
   63 			echo "$NAME still running with pid $pid" >&2
   64 		else
   65 			[ -n "$PIDFILE" ] && rm -f "$PIDFILE"
   66 		fi
   67 	else
   68 		echo "$NAME is not running" >&2
   69 	fi
   70 	;;
   71 restart)
   72 	$0 stop && \
   73 	$0 start
   74 	;;
   75 restartnice)
   76 	$0 stopnice && \
   77 	$0 start
   78 	;;
   79 status)
   80 	pid=$(getpid)
   81 	if [ -n "$pid" ]; then
   82 		echo "$NAME is running with pid $pid"
   83 	else
   84 		echo "$NAME is not running"
   85 	fi
   86 	;;
   87 *)
   88 	echo "usage: $0 [start|stop|stopnice|restart|restartnice|status]"
   89 	;;
   90 esac

Generated by cgit