This adds support for setting several safe_asterisk parameters using
[asterisk/asterisk.git] / contrib / scripts / safe_asterisk
1 #!/bin/sh
2 # vim:textwidth=80:tabstop=4:shiftwidth=4:smartindent:autoindent
3
4 ASTSBINDIR=__ASTERISK_SBIN_DIR__
5 ASTVARRUNDIR=__ASTERISK_VARRUN_DIR__
6 ASTVARLOGDIR=__ASTERISK_LOG_DIR__
7
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}
19 SLEEPSECS=4
20 ASTPIDFILE=${ASTVARRUNDIR}/asterisk.pid
21
22 # comment this line out to have this script _not_ kill all mpg123 processes when
23 # asterisk exits
24 KILLALLMPG123=1
25
26 # run asterisk with this priority
27 PRIORITY=0
28
29 # set system filemax on supported OSes if this variable is set
30 # SYSMAXFILES=262144
31
32 # Asterisk allows full permissions by default, so set a umask, if you want
33 # restricted permissions.
34 #UMASK=022
35
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.
38 # MAXFILES=32768
39
40 message() {
41         echo "$1" >&2
42         if test "x$SYSLOG" != "x" ; then
43             logger -p "${SYSLOG}.warn" -t safe_asterisk[$$] "$1"
44         fi
45         if test "x$LOGFILE" != "x" ; then
46             echo "safe_asterisk[$$]: $1" >> "$LOGFILE"
47         fi
48 }
49
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."
55         exit 1
56 fi
57
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
60 # line.
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
65         PRIORITY=0
66         message "safe_asterisk was started by `id -n` (uid `id -u`)."
67 else
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 ))
75                         fi
76                 fi
77                 SYSCTL_MAXFILES="fs.file-max"
78         elif `uname -s | grep Darwin /dev/null 2>&1`; then
79                 SYSCTL_MAXFILES="kern.maxfiles"
80         fi
81
82
83         if test "x$SYSMAXFILES" != "x"; then
84                 if test "x$SYSCTL_MAXFILES" != "x"; then
85                         sysctl -w $SYSCTL_MAXFILES=$SYSMAXFILES
86                 fi
87         fi
88
89         # set the process's filemax to whatever set above
90         ulimit -n $MAXFILES
91
92         if test ! -d ${ASTVARRUNDIR} ; then
93                 mkdir -p ${ASTVARRUNDIR}
94                 chmod 770 ${ASTVARRUNDIR}
95         fi
96
97 fi
98
99 if test "x$UMASK" != "x"; then
100         umask $UMASK
101 fi
102
103 #
104 # Let Asterisk dump core
105 #
106 ulimit -c unlimited
107
108 #
109 # Don't fork when running "safely"
110 #
111 ASTARGS=""
112 if test "x$TTY" != "x" ; then
113         if test -c /dev/tty${TTY} ; then
114                 TTY=tty${TTY}
115         elif test -c /dev/vc/${TTY} ; then
116                 TTY=vc/${TTY}
117         else
118                 message "Cannot find specified TTY (${TTY})"
119                 exit 1
120         fi
121         ASTARGS="${ASTARGS} -vvvg"
122         if test "x$CONSOLE" != "xno" ; then
123                 ASTARGS="${ASTARGS} -c"
124         fi
125 fi
126
127 if test ! -d "${RUNDIR}" ; then
128         message "${RUNDIR} does not exist, creating"
129         mkdir -p "${RUNDIR}"
130         if test ! -d "${RUNDIR}" ; then
131                 message "Unable to create ${RUNDIR}"
132                 exit 1
133         fi
134 fi
135
136 if test ! -w "${DUMPDROP}" ; then       
137         message "Cannot write to ${DUMPDROP}"
138         exit 1
139 fi
140
141 #
142 # Don't die if stdout/stderr can't be written to
143 #
144 trap '' PIPE
145
146 #
147 # Run scripts to set any environment variables or do any other system-specific setup needed
148 #
149
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
153                         . ${script}
154                 fi
155         done
156 fi
157
158 run_asterisk()
159 {
160         while :; do 
161
162                 if test "x$TTY" != "x" ; then
163                         cd "${RUNDIR}"
164                         stty sane < /dev/${TTY}
165                         nice -n $PRIORITY ${ASTSBINDIR}/asterisk -f ${CLIARGS} ${ASTARGS} > /dev/${TTY} 2>&1 < /dev/${TTY}
166                 else
167                         cd "${RUNDIR}"
168                         nice -n $PRIORITY ${ASTSBINDIR}/asterisk -f ${CLIARGS} ${ASTARGS}
169                 fi
170                 EXITSTATUS=$?
171                 message "Asterisk ended with exit status $EXITSTATUS"
172                 if test "x$EXITSTATUS" = "x0" ; then
173                         # Properly shutdown....
174                         message "Asterisk shutdown normally."
175                         exit 0
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"
183                         fi
184                         if test "x$EXEC" != "x" ; then
185                                 $EXEC
186                         fi
187
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 &
194                         fi
195                 else
196                         message "Asterisk died with code $EXITSTATUS."
197
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 &
204                         fi
205                 fi
206                 message "Automatically restarting Asterisk."
207                 sleep $SLEEPSECS
208                 if test "0$KILLALLMPG123" -gt "0" ; then
209                         pkill -9 mpg123
210                 fi
211         done
212 }
213
214 run_asterisk &