safe_asterisk: Cleanup and debian compatibility.

Cleans up the safe_asterisk script and adds the ASTSAFE_FOREGROUND
option that allows the debian asterisk init script to capture the
right pid.

* Drop the vim #modeline which wasn't used. Use test consistently
  without the odd configure xno syntax. Double quote all paths.
  General cleanup.
* Don't output message()s to the console but only to TTY if set.
* Allow TTY to be "no" as well as empty (debian compatibility with
  debian/patches/safe_asterisk-config).
* Add option to export ASTSAFE_FOREGROUND=1 from the init script
  that calls this to disable backgrounding. Debian uses a similar
  method in debian/patches/safe_asterisk-nobg).

ASTERISK-23492 #close
Review: https://reviewboard.asterisk.org/r/3574/


git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/1.8@415132 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Walter Doekes
2014-06-04 15:16:04 +00:00
parent 45c62c1733
commit 14cfff4a02

View File

@@ -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}
nice -n $PRIORITY "${ASTSBINDIR}/asterisk" -f ${CLIARGS} ${ASTARGS} >/dev/${TTY} 2>&1 </dev/${TTY}
else
cd /tmp
nice -n $PRIORITY ${ASTSBINDIR}/asterisk -f ${CLIARGS} ${ASTARGS}
nice -n $PRIORITY "${ASTSBINDIR}/asterisk" -f ${CLIARGS} ${ASTARGS} >/dev/null 2>&1 </dev/null
fi
EXITSTATUS=$?
message "Asterisk ended with exit status $EXITSTATUS"
if test "x$EXITSTATUS" = "x0" ; then
if test $EXITSTATUS -eq 0; then
# Properly shutdown....
message "Asterisk shutdown normally."
exit 0
elif test "0$EXITSTATUS" -gt "128" ; then
EXITSIGNAL=$(($EXITSTATUS - 128))
elif test $EXITSTATUS -gt 128; then
EXITSIGNAL=$((EXITSTATUS - 128))
echo "Asterisk exited on signal $EXITSIGNAL."
if test "x$NOTIFY" != "x" ; then
if test -n "$NOTIFY"; then
echo "Asterisk on $MACHINE exited on signal $EXITSIGNAL. Might want to take a peek." | \
mail -s "Asterisk Died" $NOTIFY
message "Exited on signal $EXITSIGNAL"
fi
if test "x$EXEC" != "x" ; then
if test -n "$EXEC"; then
$EXEC
fi
PID=`cat ${ASTPIDFILE}`
DATE=`date "+%Y-%m-%dT%H:%M:%S%z"`
if test -f /tmp/core.${PID} ; then
mv /tmp/core.${PID} ${DUMPDROP}/core.`hostname`-$DATE &
elif test -f /tmp/core ; then
mv /tmp/core ${DUMPDROP}/core.`hostname`-$DATE &
if test -f /tmp/core.${PID}; then
mv /tmp/core.${PID} "${DUMPDROP}/core.`hostname`-$DATE" &
elif test -f /tmp/core; then
mv /tmp/core "${DUMPDROP}/core.`hostname`-$DATE" &
fi
else
message "Asterisk died with code $EXITSTATUS."
PID=`cat ${ASTPIDFILE}`
DATE=`date "+%Y-%m-%dT%H:%M:%S%z"`
if test -f /tmp/core.${PID} ; then
mv /tmp/core.${PID} ${DUMPDROP}/core.`hostname`-$DATE &
elif test -f /tmp/core ; then
mv /tmp/core ${DUMPDROP}/core.`hostname`-$DATE &
if test -f /tmp/core.${PID}; then
mv /tmp/core.${PID} "${DUMPDROP}/core.`hostname`-$DATE" &
elif test -f /tmp/core; then
mv /tmp/core "${DUMPDROP}/core.`hostname`-$DATE" &
fi
fi
message "Automatically restarting Asterisk."
sleep $SLEEPSECS
if test "0$KILLALLMPG123" -gt "0" ; then
if test "0$KILLALLMPG123" -gt 0; then
pkill -9 mpg123
fi
done
}
run_asterisk &
if test -n "$ASTSAFE_FOREGROUND"; then
run_asterisk
else
run_asterisk &
fi