2 # vim:textwidth=80:tabstop=4:shiftwidth=4:smartindent:autoindent
4 CLIARGS
="$*" # Grab any args passed to safe_asterisk
5 TTY
=9 # TTY (if you want one) for Asterisk to run on
6 CONSOLE
=yes # Whether or not you want a console
7 #NOTIFY=ben@alkaloid.net # Who to notify about crashes
8 #EXEC=/path/to/somescript # Run this command if Asterisk crashes
9 MACHINE
=`hostname` # To specify which machine has crashed when getting the mail
12 ASTSBINDIR
=__ASTERISK_SBIN_DIR__
13 ASTPIDFILE
=__ASTERISK_VARRUN_DIR__
/asterisk.pid
15 # comment this line out to have this script _not_ kill all mpg123 processes when
19 # run asterisk with this priority
22 # set system filemax on supported OSes if this variable is set
25 # set max files open with ulimit. On linux systems, this will be automatically
26 # set to the system's maximum files open devided by two, if not set here.
29 # Check if Asterisk is already running. If it is, then bug out, because
30 # starting safe_asterisk when Asterisk is running is very bad.
31 VERSION
=`${ASTSBINDIR}/asterisk -rx 'core show version'`
32 if [ "${VERSION:0:8}" = "Asterisk" ]; then # otherwise "Unable t"
33 echo "Asterisk is already running. $0 will exit now."
37 # since we're going to change priority and open files limits, we need to be
38 # root. if running asterisk as other users, pass that to asterisk on the command
40 # if we're not root, fall back to standard everything.
43 echo "Oops. I'm not root. Falling back to standard prio and file max." >&2
44 echo "This is NOT suitable for large systems." >&2
47 if `echo $OSTYPE | grep linux 2>&1 > /dev/null `
49 # maximum number of open files is set to the system maximum divided by two if
50 # MAXFILES is not set.
51 if [ "$MAXFILES" = "" ]
53 # just check if file-max is readable
54 if [ -r /proc
/sys
/fs
/file-max
]
56 MAXFILES
=$
(( `cat /proc/sys/fs/file-max` / 2 ))
59 SYSCTL_MAXFILES
="fs.file-max"
60 elif `echo $OSTYPE | grep darwin 2>&1 > /dev/null `
62 SYSCTL_MAXFILES
="kern.maxfiles"
66 if [ "$SYSMAXFILES" != "" ]
68 if [ "$SYSCTL_MAXFILES" != "" ]
70 sysctl
-w $SYSCTL_MAXFILES=$SYSMAXFILES
74 # set the process's filemax to whatever set above
80 # Let Asterisk dump core
85 # Don't fork when running "safely"
88 if [ "$TTY" != "" ]; then
89 if [ -c /dev
/tty
${TTY} ]; then
91 elif [ -c /dev
/vc
/${TTY} ]; then
94 echo "Cannot find your TTY (${TTY})" >&2
97 ASTARGS
="${ASTARGS} -vvvg"
98 if [ "$CONSOLE" != "no" ]; then
99 ASTARGS
="${ASTARGS} -c"
102 if [ ! -w ${DUMPDROP} ]; then
103 echo "Cannot write to ${DUMPDROP}" >&2
108 # Don't die if stdout/stderr can't be written to
113 # Run scripts to set any environment variables or do any other system-specific setup needed
116 if [ -d /etc
/asterisk
/startup.d
]; then
117 for script in /etc
/asterisk
/startup.d
/*.sh
; do
118 if [ -x ${script} ]; then
128 if [ "$TTY" != "" ]; then
130 stty sane
< /dev
/${TTY}
131 nice
-n $PRIORITY ${ASTSBINDIR}/asterisk -f ${CLIARGS} ${ASTARGS} >& /dev/${TTY} < /dev/${TTY}
134 nice
-n $PRIORITY ${ASTSBINDIR}/asterisk -f ${CLIARGS} ${ASTARGS}
137 echo "Asterisk ended with exit status $EXITSTATUS"
138 if [ "$EXITSTATUS" = "0" ]; then
139 # Properly shutdown....
140 echo "Asterisk shutdown normally."
142 elif [ $EXITSTATUS -gt 128 ]; then
143 let EXITSIGNAL
=EXITSTATUS-128
144 echo "Asterisk exited on signal $EXITSIGNAL."
145 if [ "$NOTIFY" != "" ]; then
146 echo "Asterisk on $MACHINE exited on signal $EXITSIGNAL. Might want to take a peek." | \
147 mail -s "Asterisk Died" $NOTIFY
149 if [ "$EXEC" != "" ]; then
153 PID
=`cat ${ASTPIDFILE}`
154 if [ -f /tmp
/core.
${PID} ]; then
155 mv /tmp
/core.
${PID} ${DUMPDROP}/core.
`hostname`-`date -Iseconds` &
156 elif [ -f /tmp
/core
]; then
157 mv /tmp
/core
${DUMPDROP}/core.
`hostname`-`date -Iseconds` &
160 echo "Asterisk died with code $EXITSTATUS."
162 PID
=`cat ${ASTPIDFILE}`
163 if [ -f /tmp
/core.
${PID} ]; then
164 mv /tmp
/core.
${PID} ${DUMPDROP}/core.
`hostname`-`date -Iseconds` &
165 elif [ -f /tmp
/core
]; then
166 mv /tmp
/core
${DUMPDROP}/core.
`hostname`-`date -Iseconds` &
169 echo "Automatically restarting Asterisk."
171 if [ $KILLALLMPG123 ]