Merged revisions 239307 via svnmerge from
[asterisk/asterisk.git] / contrib / scripts / safe_asterisk
1 #!/bin/sh
2 # vim:textwidth=80:tabstop=4:shiftwidth=4:smartindent:autoindent
3
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 #LOGFILE=/path/to/logfile       # Where to place the normal logfile (disabled if blank)
10 #SYSLOG=local0                          # Which syslog facility to use (disabled if blank)
11 MACHINE=`hostname`                      # To specify which machine has crashed when getting the mail
12 DUMPDROP=/tmp
13 SLEEPSECS=4
14 ASTSBINDIR=__ASTERISK_SBIN_DIR__
15 ASTVARRUNDIR=__ASTERISK_VARRUN_DIR__
16 ASTPIDFILE=${ASTVARRUNDIR}/asterisk.pid
17
18 # comment this line out to have this script _not_ kill all mpg123 processes when
19 # asterisk exits
20 KILLALLMPG123=1
21
22 # run asterisk with this priority
23 PRIORITY=0
24
25 # set system filemax on supported OSes if this variable is set
26 # SYSMAXFILES=262144
27
28 # Asterisk allows full permissions by default, so set a umask, if you want
29 # restricted permissions.
30 #UMASK=022
31
32 # set max files open with ulimit. On linux systems, this will be automatically
33 # set to the system's maximum files open devided by two, if not set here.
34 # MAXFILES=32768
35
36 message() {
37         echo "$1" >&2
38         if test "x$SYSLOG" != "x" ; then
39             logger -p "${SYSLOG}.warn" -t safe_asterisk[$$] "$1"
40         fi
41         if test "x$LOGFILE" != "x" ; then
42             echo "safe_asterisk[$$]: $1" >> "$LOGFILE"
43         fi
44 }
45
46 # Check if Asterisk is already running.  If it is, then bug out, because
47 # starting safe_asterisk when Asterisk is running is very bad.
48 VERSION=`${ASTSBINDIR}/asterisk -rx 'core show version'`
49 if test "`echo $VERSION | cut -c 1-8`" = "Asterisk" ; then
50         message "Asterisk is already running.  $0 will exit now."
51         exit 1
52 fi
53
54 # since we're going to change priority and open files limits, we need to be
55 # root. if running asterisk as other users, pass that to asterisk on the command
56 # line.
57 # if we're not root, fall back to standard everything.
58 if test `id -u` != 0 ; then
59         echo "Oops. I'm not root. Falling back to standard prio and file max." >&2
60         echo "This is NOT suitable for large systems." >&2
61         PRIORITY=0
62         message "safe_asterisk was started by `id -n` (uid `id -u`)."
63 else
64         if `uname -s | grep Linux >/dev/null 2>&1`; then
65                 # maximum number of open files is set to the system maximum divided by two if
66                 # MAXFILES is not set.
67                 if test "x$MAXFILES" = "x" ; then
68                         # just check if file-max is readable
69                         if test -r /proc/sys/fs/file-max ; then
70                                 MAXFILES=$(( `cat /proc/sys/fs/file-max` / 2 ))
71                         fi
72                 fi
73                 SYSCTL_MAXFILES="fs.file-max"
74         elif `uname -s | grep Darwin /dev/null 2>&1`; then
75                 SYSCTL_MAXFILES="kern.maxfiles"
76         fi
77
78
79         if test "x$SYSMAXFILES" != "x"; then
80                 if test "x$SYSCTL_MAXFILES" != "x"; then
81                         sysctl -w $SYSCTL_MAXFILES=$SYSMAXFILES
82                 fi
83         fi
84
85         # set the process's filemax to whatever set above
86         ulimit -n $MAXFILES
87
88         if test ! -d ${ASTVARRUNDIR} ; then
89                 mkdir -p ${ASTVARRUNDIR}
90                 chmod 770 ${ASTVARRUNDIR}
91         fi
92
93 fi
94
95 if test "x$UMASK" != "x"; then
96         umask $UMASK
97 fi
98
99 #
100 # Let Asterisk dump core
101 #
102 ulimit -c unlimited
103
104 #
105 # Don't fork when running "safely"
106 #
107 ASTARGS=""
108 if test "x$TTY" != "x" ; then
109         if test -c /dev/tty${TTY} ; then
110                 TTY=tty${TTY}
111         elif test -c /dev/vc/${TTY} ; then
112                 TTY=vc/${TTY}
113         else
114                 message "Cannot find specified TTY (${TTY})"
115                 exit 1
116         fi
117         ASTARGS="${ASTARGS} -vvvg"
118         if test "x$CONSOLE" != "xno" ; then
119                 ASTARGS="${ASTARGS} -c"
120         fi
121 fi
122 if test ! -w "${DUMPDROP}" ; then       
123         message "Cannot write to ${DUMPDROP}"
124         exit 1
125 fi
126
127 #
128 # Don't die if stdout/stderr can't be written to
129 #
130 trap '' PIPE
131
132 #
133 # Run scripts to set any environment variables or do any other system-specific setup needed
134 #
135
136 if test -d /etc/asterisk/startup.d ; then
137         for script in /etc/asterisk/startup.d/*.sh; do
138                 if test -r ${script} ; then
139                         . ${script}
140                 fi
141         done
142 fi
143
144 run_asterisk()
145 {
146         while :; do 
147
148                 if test "x$TTY" != "x" ; then
149                         cd /tmp
150                         stty sane < /dev/${TTY}
151                         nice -n $PRIORITY ${ASTSBINDIR}/asterisk -f ${CLIARGS} ${ASTARGS} > /dev/${TTY} 2>&1 < /dev/${TTY}
152                 else
153                         cd /tmp
154                         nice -n $PRIORITY ${ASTSBINDIR}/asterisk -f ${CLIARGS} ${ASTARGS}
155                 fi
156                 EXITSTATUS=$?
157                 message "Asterisk ended with exit status $EXITSTATUS"
158                 if test "x$EXITSTATUS" = "x0" ; then
159                         # Properly shutdown....
160                         message "Asterisk shutdown normally."
161                         exit 0
162                 elif test "0$EXITSTATUS" -gt "128" ; then
163                         EXITSIGNAL=$(($EXITSTATUS - 128))
164                         echo "Asterisk exited on signal $EXITSIGNAL."
165                         if test "x$NOTIFY" != "x" ; then
166                                 echo "Asterisk on $MACHINE exited on signal $EXITSIGNAL.  Might want to take a peek." | \
167                                 mail -s "Asterisk Died" $NOTIFY
168                                 message "Exited on signal $EXITSIGNAL"
169                         fi
170                         if test "x$EXEC" != "x" ; then
171                                 $EXEC
172                         fi
173
174                         PID=`cat ${ASTPIDFILE}`
175                         DATE=`date "+%Y-%m-%dT%H:%M:%S%z"`
176                         if test -f /tmp/core.${PID} ; then
177                                 mv /tmp/core.${PID} ${DUMPDROP}/core.`hostname`-$DATE &
178                         elif test -f /tmp/core ; then
179                                 mv /tmp/core ${DUMPDROP}/core.`hostname`-$DATE &
180                         fi
181                 else
182                         message "Asterisk died with code $EXITSTATUS."
183
184                         PID=`cat ${ASTPIDFILE}`
185                         DATE=`date "+%Y-%m-%dT%H:%M:%S%z"`
186                         if test -f /tmp/core.${PID} ; then
187                                 mv /tmp/core.${PID} ${DUMPDROP}/core.`hostname`-$DATE &
188                         elif test -f /tmp/core ; then
189                                 mv /tmp/core ${DUMPDROP}/core.`hostname`-$DATE &
190                         fi
191                 fi
192                 message "Automatically restarting Asterisk."
193                 sleep $SLEEPSECS
194                 if test "0$KILLALLMPG123" -gt "0" ; then
195                         pkill -9 mpg123
196                 fi
197         done
198 }
199
200 run_asterisk &