Fix a few things I missed to ensure zt_chan_conf structure is not modified in mkintf
[asterisk-bristuff.git] / contrib / scripts / safe_asterisk
blob1cb5e8006bd1bafaffaaeb11c3dd928d95ed685d
1 #!/bin/bash
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
10 DUMPDROP=/tmp
11 SLEEPSECS=4
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
16 # asterisk exits
17 KILLALLMPG123=1
19 # run asterisk with this priority
20 PRIORITY=0
22 # set system filemax on supported OSes if this variable is set
23 # SYSMAXFILES=262144
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.
27 # MAXFILES=32768
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."
34 exit 1
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
39 # line.
40 # if we're not root, fall back to standard everything.
41 if [ `id -u` != 0 ]
42 then
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
45 PRIORITY=0
46 else
47 if `echo $OSTYPE | grep linux 2>&1 > /dev/null `
48 then
49 # maximum number of open files is set to the system maximum divided by two if
50 # MAXFILES is not set.
51 if [ "$MAXFILES" = "" ]
52 then
53 # just check if file-max is readable
54 if [ -r /proc/sys/fs/file-max ]
55 then
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 `
61 then
62 SYSCTL_MAXFILES="kern.maxfiles"
66 if [ "$SYSMAXFILES" != "" ]
67 then
68 if [ "$SYSCTL_MAXFILES" != "" ]
69 then
70 sysctl -w $SYSCTL_MAXFILES=$SYSMAXFILES
74 # set the process's filemax to whatever set above
75 ulimit -n $MAXFILES
80 # Let Asterisk dump core
82 ulimit -c unlimited
85 # Don't fork when running "safely"
87 ASTARGS=""
88 if [ "$TTY" != "" ]; then
89 if [ -c /dev/tty${TTY} ]; then
90 TTY=tty${TTY}
91 elif [ -c /dev/vc/${TTY} ]; then
92 TTY=vc/${TTY}
93 else
94 echo "Cannot find your TTY (${TTY})" >&2
95 exit 1
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
104 exit 1
108 # Don't die if stdout/stderr can't be written to
110 trap '' PIPE
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
119 source ${script}
121 done
124 run_asterisk()
126 while :; do
128 if [ "$TTY" != "" ]; then
129 cd /tmp
130 stty sane < /dev/${TTY}
131 nice -n $PRIORITY ${ASTSBINDIR}/asterisk -f ${CLIARGS} ${ASTARGS} >& /dev/${TTY} < /dev/${TTY}
132 else
133 cd /tmp
134 nice -n $PRIORITY ${ASTSBINDIR}/asterisk -f ${CLIARGS} ${ASTARGS}
136 EXITSTATUS=$?
137 echo "Asterisk ended with exit status $EXITSTATUS"
138 if [ "$EXITSTATUS" = "0" ]; then
139 # Properly shutdown....
140 echo "Asterisk shutdown normally."
141 exit 0
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
150 $EXEC
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` &
159 else
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."
170 sleep $SLEEPSECS
171 if [ $KILLALLMPG123 ]
172 then
173 killall -9 mpg123
175 done
178 run_asterisk &