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