diff --git a/contrib/scripts/safe_asterisk b/contrib/scripts/safe_asterisk index 64c41b4799..1241396b62 100644 --- a/contrib/scripts/safe_asterisk +++ b/contrib/scripts/safe_asterisk @@ -1,19 +1,18 @@ #!/bin/sh -# vim:textwidth=80:tabstop=4:shiftwidth=4:smartindent:autoindent -CLIARGS="$*" # Grab any args passed to safe_asterisk -TTY=9 # TTY (if you want one) for Asterisk to run on -CONSOLE=yes # Whether or not you want a console +CLIARGS="$*" # Grab any args passed to safe_asterisk +TTY=9 # TTY (if you want one) for Asterisk to run on +CONSOLE=yes # Whether or not you want a console #NOTIFY=ben@alkaloid.net # Who to notify about crashes #EXEC=/path/to/somescript # Run this command if Asterisk crashes #LOGFILE=/path/to/logfile # Where to place the normal logfile (disabled if blank) -#SYSLOG=local0 # Which syslog facility to use (disabled if blank) -MACHINE=`hostname` # To specify which machine has crashed when getting the mail +#SYSLOG=local0 # Which syslog facility to use (disabled if blank) +MACHINE=`hostname` # To specify which machine has crashed when getting the mail DUMPDROP=/tmp SLEEPSECS=4 -ASTSBINDIR=__ASTERISK_SBIN_DIR__ -ASTVARRUNDIR=__ASTERISK_VARRUN_DIR__ -ASTPIDFILE=${ASTVARRUNDIR}/asterisk.pid +ASTSBINDIR="__ASTERISK_SBIN_DIR__" +ASTVARRUNDIR="__ASTERISK_VARRUN_DIR__" +ASTPIDFILE="${ASTVARRUNDIR}/asterisk.pid" # comment this line out to have this script _not_ kill all mpg123 processes when # asterisk exits @@ -34,19 +33,21 @@ PRIORITY=0 # MAXFILES=32768 message() { - echo "$1" >&2 - if test "x$SYSLOG" != "x" ; then - logger -p "${SYSLOG}.warn" -t safe_asterisk[$$] "$1" + if test -n "$TTY" && test "$TTY" != "no"; then + echo "$1" >/dev/${TTY} fi - if test "x$LOGFILE" != "x" ; then - echo "safe_asterisk[$$]: $1" >> "$LOGFILE" + if test -n "$SYSLOG"; then + logger -p "${SYSLOG}.warn" -t safe_asterisk[$$] "$1" + fi + if test -n "$LOGFILE"; then + echo "safe_asterisk[$$]: $1" >>"$LOGFILE" fi } # Check if Asterisk is already running. If it is, then bug out, because # starting safe_asterisk when Asterisk is running is very bad. -VERSION=`${ASTSBINDIR}/asterisk -nrx 'core show version' 2>/dev/null` -if test "`echo $VERSION | cut -c 1-8`" = "Asterisk" ; then +VERSION=`"${ASTSBINDIR}/asterisk" -nrx 'core show version' 2>/dev/null` +if test "`echo $VERSION | cut -c 1-8`" = "Asterisk"; then message "Asterisk is already running. $0 will exit now." exit 1 fi @@ -55,7 +56,7 @@ fi # root. if running asterisk as other users, pass that to asterisk on the command # line. # if we're not root, fall back to standard everything. -if test `id -u` != 0 ; then +if test `id -u` != 0; then echo "Oops. I'm not root. Falling back to standard prio and file max." >&2 echo "This is NOT suitable for large systems." >&2 PRIORITY=0 @@ -64,9 +65,9 @@ else if `uname -s | grep Linux >/dev/null 2>&1`; then # maximum number of open files is set to the system maximum divided by two if # MAXFILES is not set. - if test "x$MAXFILES" = "x" ; then + if test -z "$MAXFILES"; then # just check if file-max is readable - if test -r /proc/sys/fs/file-max ; then + if test -r /proc/sys/fs/file-max; then MAXFILES=$(( `cat /proc/sys/fs/file-max` / 2 )) fi fi @@ -76,8 +77,8 @@ else fi - if test "x$SYSMAXFILES" != "x"; then - if test "x$SYSCTL_MAXFILES" != "x"; then + if test -n "$SYSMAXFILES"; then + if test -n "$SYSCTL_MAXFILES"; then sysctl -w $SYSCTL_MAXFILES=$SYSMAXFILES fi fi @@ -85,14 +86,14 @@ else # set the process's filemax to whatever set above ulimit -n $MAXFILES - if test ! -d ${ASTVARRUNDIR} ; then - mkdir -p ${ASTVARRUNDIR} - chmod 770 ${ASTVARRUNDIR} + if test ! -d "${ASTVARRUNDIR}"; then + mkdir -p "${ASTVARRUNDIR}" + chmod 770 "${ASTVARRUNDIR}" fi fi -if test "x$UMASK" != "x"; then +if test -n "$UMASK"; then umask $UMASK fi @@ -105,21 +106,21 @@ ulimit -c unlimited # Don't fork when running "safely" # ASTARGS="" -if test "x$TTY" != "x" ; then - if test -c /dev/tty${TTY} ; then +if test -n "$TTY" && test "$TTY" != "no"; then + if test -c /dev/tty${TTY}; then TTY=tty${TTY} - elif test -c /dev/vc/${TTY} ; then + elif test -c /dev/vc/${TTY}; then TTY=vc/${TTY} else message "Cannot find specified TTY (${TTY})" exit 1 fi ASTARGS="${ASTARGS} -vvvg" - if test "x$CONSOLE" != "xno" ; then + if test "$CONSOLE" != "no"; then ASTARGS="${ASTARGS} -c" fi fi -if test ! -w "${DUMPDROP}" ; then +if test ! -w "${DUMPDROP}"; then message "Cannot write to ${DUMPDROP}" exit 1 fi @@ -133,9 +134,9 @@ trap '' PIPE # Run scripts to set any environment variables or do any other system-specific setup needed # -if test -d /etc/asterisk/startup.d ; then +if test -d /etc/asterisk/startup.d; then for script in /etc/asterisk/startup.d/*.sh; do - if test -r ${script} ; then + if test -r ${script}; then . ${script} fi done @@ -143,58 +144,61 @@ fi run_asterisk() { - while :; do - - if test "x$TTY" != "x" ; then + while :; do + if test -n "$TTY" && test "$TTY" != "no"; then cd /tmp - stty sane < /dev/${TTY} - nice -n $PRIORITY ${ASTSBINDIR}/asterisk -f ${CLIARGS} ${ASTARGS} > /dev/${TTY} 2>&1 < /dev/${TTY} + stty sane /dev/${TTY} 2>&1 /dev/null 2>&1