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