2 # vim:textwidth=80:tabstop=4:shiftwidth=4:smartindent:autoindent
4 ASTSBINDIR=__ASTERISK_SBIN_DIR__
5 ASTVARRUNDIR=__ASTERISK_VARRUN_DIR__
6 ASTVARLOGDIR=__ASTERISK_LOG_DIR__
8 CLIARGS="$*" # Grab any args passed to safe_asterisk
9 TTY=9 # TTY (if you want one) for Asterisk to run on
10 CONSOLE=yes # Whether or not you want a console
11 #NOTIFY=root@localhost.localdomain # Who to notify about crashes
12 NOTIFY=${NOTIFY:-} # Who to notify about crashes
13 #EXEC=/path/to/somescript # Run this command if Asterisk crashes
14 #LOGFILE=${ASTVARLOGDIR}/safe_asterisk.log # Where to place the normal logfile (disabled if blank)
15 SYSLOG=${SYSLOG:-} # Which syslog facility to use (disabled if blank)
16 MACHINE=`hostname` # To specify which machine has crashed when getting the mail
17 DUMPDROP=${DUMPDROP:-/tmp}
18 RUNDIR=${RUNDIR:-/tmp}
20 ASTPIDFILE=${ASTVARRUNDIR}/asterisk.pid
22 # comment this line out to have this script _not_ kill all mpg123 processes when
26 # run asterisk with this priority
29 # set system filemax on supported OSes if this variable is set
32 # Asterisk allows full permissions by default, so set a umask, if you want
33 # restricted permissions.
36 # set max files open with ulimit. On linux systems, this will be automatically
37 # set to the system's maximum files open devided by two, if not set here.
42 if test "x$SYSLOG" != "x" ; then
43 logger -p "${SYSLOG}.warn" -t safe_asterisk[$$] "$1"
45 if test "x$LOGFILE" != "x" ; then
46 echo "safe_asterisk[$$]: $1" >> "$LOGFILE"
50 # Check if Asterisk is already running. If it is, then bug out, because
51 # starting safe_asterisk when Asterisk is running is very bad.
52 VERSION=`${ASTSBINDIR}/asterisk -nrx 'core show version'`
53 if test "`echo $VERSION | cut -c 1-8`" = "Asterisk" ; then
54 message "Asterisk is already running. $0 will exit now."
58 # since we're going to change priority and open files limits, we need to be
59 # root. if running asterisk as other users, pass that to asterisk on the command
61 # if we're not root, fall back to standard everything.
62 if test `id -u` != 0 ; then
63 echo "Oops. I'm not root. Falling back to standard prio and file max." >&2
64 echo "This is NOT suitable for large systems." >&2
66 message "safe_asterisk was started by `id -n` (uid `id -u`)."
68 if `uname -s | grep Linux >/dev/null 2>&1`; then
69 # maximum number of open files is set to the system maximum divided by two if
70 # MAXFILES is not set.
71 if test "x$MAXFILES" = "x" ; then
72 # just check if file-max is readable
73 if test -r /proc/sys/fs/file-max ; then
74 MAXFILES=$(( `cat /proc/sys/fs/file-max` / 2 ))
77 SYSCTL_MAXFILES="fs.file-max"
78 elif `uname -s | grep Darwin /dev/null 2>&1`; then
79 SYSCTL_MAXFILES="kern.maxfiles"
83 if test "x$SYSMAXFILES" != "x"; then
84 if test "x$SYSCTL_MAXFILES" != "x"; then
85 sysctl -w $SYSCTL_MAXFILES=$SYSMAXFILES
89 # set the process's filemax to whatever set above
92 if test ! -d ${ASTVARRUNDIR} ; then
93 mkdir -p ${ASTVARRUNDIR}
94 chmod 770 ${ASTVARRUNDIR}
99 if test "x$UMASK" != "x"; then
104 # Let Asterisk dump core
109 # Don't fork when running "safely"
112 if test "x$TTY" != "x" ; then
113 if test -c /dev/tty${TTY} ; then
115 elif test -c /dev/vc/${TTY} ; then
118 message "Cannot find specified TTY (${TTY})"
121 ASTARGS="${ASTARGS} -vvvg"
122 if test "x$CONSOLE" != "xno" ; then
123 ASTARGS="${ASTARGS} -c"
127 if test ! -d "${RUNDIR}" ; then
128 message "${RUNDIR} does not exist, creating"
130 if test ! -d "${RUNDIR}" ; then
131 message "Unable to create ${RUNDIR}"
136 if test ! -w "${DUMPDROP}" ; then
137 message "Cannot write to ${DUMPDROP}"
142 # Don't die if stdout/stderr can't be written to
147 # Run scripts to set any environment variables or do any other system-specific setup needed
150 if test -d /etc/asterisk/startup.d ; then
151 for script in /etc/asterisk/startup.d/*.sh; do
152 if test -r ${script} ; then
162 if test "x$TTY" != "x" ; then
164 stty sane < /dev/${TTY}
165 nice -n $PRIORITY ${ASTSBINDIR}/asterisk -f ${CLIARGS} ${ASTARGS} > /dev/${TTY} 2>&1 < /dev/${TTY}
168 nice -n $PRIORITY ${ASTSBINDIR}/asterisk -f ${CLIARGS} ${ASTARGS}
171 message "Asterisk ended with exit status $EXITSTATUS"
172 if test "x$EXITSTATUS" = "x0" ; then
173 # Properly shutdown....
174 message "Asterisk shutdown normally."
176 elif test "0$EXITSTATUS" -gt "128" ; then
177 EXITSIGNAL=$(($EXITSTATUS - 128))
178 echo "Asterisk exited on signal $EXITSIGNAL."
179 if test "x$NOTIFY" != "x" ; then
180 echo "Asterisk on $MACHINE exited on signal $EXITSIGNAL. Might want to take a peek." | \
181 mail -s "Asterisk Died" $NOTIFY
182 message "Exited on signal $EXITSIGNAL"
184 if test "x$EXEC" != "x" ; then
188 PID=`cat ${ASTPIDFILE}`
189 DATE=`date "+%Y-%m-%dT%H:%M:%S%z"`
190 if test -f ${RUNDIR}/core.${PID} ; then
191 mv ${RUNDIR}/core.${PID} ${DUMPDROP}/core.`hostname`-$DATE &
192 elif test -f ${RUNDIR}/core ; then
193 mv ${RUNDIR}/core ${DUMPDROP}/core.`hostname`-$DATE &
196 message "Asterisk died with code $EXITSTATUS."
198 PID=`cat ${ASTPIDFILE}`
199 DATE=`date "+%Y-%m-%dT%H:%M:%S%z"`
200 if test -f ${RUNDIR}/core.${PID} ; then
201 mv ${RUNDIR}/core.${PID} ${DUMPDROP}/core.`hostname`-$DATE &
202 elif test -f ${RUNDIR}/core ; then
203 mv ${RUNDIR}/core ${DUMPDROP}/core.`hostname`-$DATE &
206 message "Automatically restarting Asterisk."
208 if test "0$KILLALLMPG123" -gt "0" ; then