Index: contrib/scripts/safe_asterisk =================================================================== --- contrib/scripts/safe_asterisk (revision 81712) +++ contrib/scripts/safe_asterisk (working copy) @@ -6,6 +6,8 @@ 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 DUMPDROP=/tmp SLEEPSECS=4 @@ -26,15 +28,26 @@ # set to the system's maximum files open devided by two, if not set here. # MAXFILES=32768 +function message() { + echo "$1" >&2 + if [ "$SYSLOG" != "" ]; then + logger -p "${SYSLOG}.warn" -t safe_asterisk[$$] "$1" + fi + if [ "$LOGFILE" != "" ]; then + echo "safe_asterisk[$$]: $1" >> "$LOGFILE" + fi +} + # since we're going to change priority and open files limits, we need to be # 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 [ `id -u` != 0 ] then - echo "Ops. I'm not root. Falling back to standard prio and file max." >&2 + 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 + message "safe_asterisk was started by `id -n` (uid `id -u`)." else if `echo $OSTYPE | grep linux 2>&1 > /dev/null ` then @@ -83,7 +96,7 @@ elif [ -c /dev/vc/${TTY} ]; then TTY=vc/${TTY} else - echo "Cannot find your TTY (${TTY})" >&2 + message "Cannot find specified TTY (${TTY})" exit 1 fi ASTARGS="${ASTARGS} -vvvg" @@ -92,7 +105,7 @@ fi fi if [ ! -w ${DUMPDROP} ]; then - echo "Cannot write to ${DUMPDROP}" >&2 + message "Cannot write to ${DUMPDROP}" exit 1 fi @@ -126,10 +139,10 @@ nice -n $PRIORITY ${ASTSBINDIR}/asterisk -f ${CLIARGS} ${ASTARGS} fi EXITSTATUS=$? - echo "Asterisk ended with exit status $EXITSTATUS" + message "Asterisk ended with exit status $EXITSTATUS" if [ "$EXITSTATUS" = "0" ]; then # Properly shutdown.... - echo "Asterisk shutdown normally." + message "Asterisk shutdown normally." exit 0 elif [ $EXITSTATUS -gt 128 ]; then let EXITSIGNAL=EXITSTATUS-128 @@ -137,6 +150,7 @@ if [ "$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 [ "$EXEC" != "" ]; then $EXEC @@ -149,21 +163,16 @@ mv /tmp/core ${DUMPDROP}/core.`hostname`-`date -Iseconds` & fi else - if [ "${EXITSTATUS}" = "0" ]; then - echo "Asterisk ended normally. Aborting." - exit 0 - else - echo "Asterisk died with code $EXITSTATUS." + message "Asterisk died with code $EXITSTATUS." - PID=`cat ${ASTPIDFILE}` - if [ -f /tmp/core.${PID} ]; then - mv /tmp/core.${PID} ${DUMPDROP}/core.`hostname`-`date -Iseconds` & - elif [ -f /tmp/core ]; then - mv /tmp/core ${DUMPDROP}/core.`hostname`-`date -Iseconds` & - fi + PID=`cat ${ASTPIDFILE}` + if [ -f /tmp/core.${PID} ]; then + mv /tmp/core.${PID} ${DUMPDROP}/core.`hostname`-`date -Iseconds` & + elif [ -f /tmp/core ]; then + mv /tmp/core ${DUMPDROP}/core.`hostname`-`date -Iseconds` & fi fi - echo "Automatically restarting Asterisk." + message "Automatically restarting Asterisk." sleep $SLEEPSECS if [ $KILLALLMPG123 ] then