Merged revisions 83316 via svnmerge from
[asterisk/asterisk.git] / contrib / scripts / safe_asterisk
1 #!/bin/bash
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 ASTPIDFILE=__ASTERISK_VARRUN_DIR__/asterisk.pid
16
17 # comment this line out to have this script _not_ kill all mpg123 processes when
18 # asterisk exits
19 KILLALLMPG123=1
20
21 # run asterisk with this priority
22 PRIORITY=0
23
24 # set system filemax on supported OSes if this variable is set
25 # SYSMAXFILES=262144
26
27 # set max files open with ulimit. On linux systems, this will be automatically
28 # set to the system's maximum files open devided by two, if not set here.
29 # MAXFILES=32768
30
31 function message() {
32         echo "$1" >&2
33         if [ "$SYSLOG" != "" ]; then
34             logger -p "${SYSLOG}.warn" -t safe_asterisk[$$] "$1"
35         fi
36         if [ "$LOGFILE" != "" ]; then
37             echo "safe_asterisk[$$]: $1" >> "$LOGFILE"
38         fi
39 }
40
41 # since we're going to change priority and open files limits, we need to be
42 # root. if running asterisk as other users, pass that to asterisk on the command
43 # line.
44 # if we're not root, fall back to standard everything.
45 if [ `id -u` != 0 ]
46 then
47         echo "Oops. I'm not root. Falling back to standard prio and file max." >&2
48         echo "This is NOT suitable for large systems." >&2
49         PRIORITY=0
50         message "safe_asterisk was started by `id -n` (uid `id -u`)."
51 else
52         if `echo $OSTYPE | grep linux 2>&1 > /dev/null `
53         then
54                 # maximum number of open files is set to the system maximum divided by two if
55                 # MAXFILES is not set.
56                 if [ "$MAXFILES" = "" ]
57                 then
58                         # just check if file-max is readable
59                         if [ -r /proc/sys/fs/file-max ]
60                         then
61                                 MAXFILES=$(( `cat /proc/sys/fs/file-max` / 2 ))
62                         fi
63                 fi
64                 SYSCTL_MAXFILES="fs.file-max"
65         elif `echo $OSTYPE | grep darwin 2>&1 > /dev/null `
66         then
67                 SYSCTL_MAXFILES="kern.maxfiles"
68         fi
69
70
71         if [ "$SYSMAXFILES" != "" ]
72         then
73                 if [ "$SYSCTL_MAXFILES" != "" ]
74                 then
75                         sysctl -w $SYSCTL_MAXFILES=$SYSMAXFILES
76                 fi
77         fi
78
79         # set the process's filemax to whatever set above
80         ulimit -n $MAXFILES
81
82 fi
83
84 #
85 # Let Asterisk dump core
86 #
87 ulimit -c unlimited
88
89 #
90 # Don't fork when running "safely"
91 #
92 ASTARGS=""
93 if [ "$TTY" != "" ]; then
94         if [ -c /dev/tty${TTY} ]; then
95                 TTY=tty${TTY}
96         elif [ -c /dev/vc/${TTY} ]; then
97                 TTY=vc/${TTY}
98         else
99                 message "Cannot find specified TTY (${TTY})"
100                 exit 1
101         fi
102         ASTARGS="${ASTARGS} -vvvg"
103         if [ "$CONSOLE" != "no" ]; then
104                 ASTARGS="${ASTARGS} -c"
105         fi
106 fi
107 if [ ! -w ${DUMPDROP} ]; then   
108         message "Cannot write to ${DUMPDROP}"
109         exit 1
110 fi
111
112 #
113 # Don't die if stdout/stderr can't be written to
114 #
115 trap '' PIPE
116
117 #
118 # Run scripts to set any environment variables or do any other system-specific setup needed
119 #
120
121 if [ -d /etc/asterisk/startup.d ]; then
122         for script in /etc/asterisk/startup.d/*.sh; do
123                 if [ -x ${script} ]; then
124                         source ${script}
125                 fi
126         done
127 fi
128
129 run_asterisk()
130 {
131         while :; do 
132
133                 if [ "$TTY" != "" ]; then
134                         cd /tmp
135                         stty sane < /dev/${TTY}
136                         nice -n $PRIORITY ${ASTSBINDIR}/asterisk -f ${CLIARGS} ${ASTARGS} >& /dev/${TTY} < /dev/${TTY}
137                 else
138                         cd /tmp
139                         nice -n $PRIORITY ${ASTSBINDIR}/asterisk -f ${CLIARGS} ${ASTARGS}
140                 fi
141                 EXITSTATUS=$?
142                 message "Asterisk ended with exit status $EXITSTATUS"
143                 if [ "$EXITSTATUS" = "0" ]; then
144                         # Properly shutdown....
145                         message "Asterisk shutdown normally."
146                         exit 0
147                 elif [ $EXITSTATUS -gt 128 ]; then
148                         let EXITSIGNAL=EXITSTATUS-128
149                         echo "Asterisk exited on signal $EXITSIGNAL."
150                         if [ "$NOTIFY" != "" ]; then
151                                 echo "Asterisk on $MACHINE exited on signal $EXITSIGNAL.  Might want to take a peek." | \
152                                 mail -s "Asterisk Died" $NOTIFY
153                                 message "Exited on signal $EXITSIGNAL"
154                         fi
155                         if [ "$EXEC" != "" ]; then
156                                 $EXEC
157                         fi
158
159                         PID=`cat ${ASTPIDFILE}`
160                         if [ -f /tmp/core.${PID} ]; then
161                                 mv /tmp/core.${PID} ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
162                         elif [ -f /tmp/core ]; then
163                                 mv /tmp/core ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
164                         fi
165                 else
166                         message "Asterisk died with code $EXITSTATUS."
167
168                         PID=`cat ${ASTPIDFILE}`
169                         if [ -f /tmp/core.${PID} ]; then
170                                 mv /tmp/core.${PID} ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
171                         elif [ -f /tmp/core ]; then
172                                 mv /tmp/core ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
173                         fi
174                 fi
175                 message "Automatically restarting Asterisk."
176                 sleep $SLEEPSECS
177                 if [ $KILLALLMPG123 ]
178                 then
179                         killall -9 mpg123
180                 fi
181         done
182 }
183
184 run_asterisk &