support ./configure --silent
[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=4
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=0
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 #
98 # Run scripts to set any environment variables or do any other system-specific setup needed
99 #
100
101 if [ -d /etc/asterisk/startup.d ]; then
102         for script in /etc/asterisk/startup.d/*.sh; do
103                 if [ -x ${script} ]; then
104                         source ${script}
105                 fi
106         done
107 fi
108
109 run_asterisk()
110 {
111         while :; do 
112
113                 if [ "$TTY" != "" ]; then
114                         cd /tmp
115                         stty sane < /dev/${TTY}
116                         nice -n $PRIORITY ${ASTSBINDIR}/asterisk ${CLIARGS} ${ASTARGS} >& /dev/${TTY} < /dev/${TTY}
117                 else
118                         cd /tmp
119                         nice -n $PRIORITY ${ASTSBINDIR}/asterisk ${CLIARGS} ${ASTARGS}
120                 fi
121                 EXITSTATUS=$?
122                 echo "Asterisk ended with exit status $EXITSTATUS"
123                 if [ "$EXITSTATUS" = "0" ]; then
124                         # Properly shutdown....
125                         echo "Asterisk shutdown normally."
126                         exit 0
127                 elif [ $EXITSTATUS -gt 128 ]; then
128                         let EXITSIGNAL=EXITSTATUS-128
129                         echo "Asterisk exited on signal $EXITSIGNAL."
130                         if [ "$NOTIFY" != "" ]; then
131                                 echo "Asterisk on $MACHINE exited on signal $EXITSIGNAL.  Might want to take a peek." | \
132                                 mail -s "Asterisk Died" $NOTIFY
133                         fi
134                         if [ -f /tmp/core ]; then
135                                 mv /tmp/core ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
136                         fi
137                 else
138                         if [ "${EXITSTATUS}" = "0" ]; then
139                                 echo "Asterisk ended normally.  Aborting."
140                                 exit 0
141                         else
142                                 echo "Asterisk died with code $EXITSTATUS."
143                                 if [ -f /tmp/core ]; then
144                                         mv /tmp/core ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
145                                 fi
146                         fi
147                 fi
148                 echo "Automatically restarting Asterisk."
149                 sleep $SLEEPSECS
150                 if [ $KILLALLMPG123 ]
151                 then
152                         killall -9 mpg123
153                 fi
154         done
155 }
156
157 run_asterisk &