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