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