Build safe_asterisk script substituting the proper path in (bug #4952, patch not...
[asterisk/asterisk.git] / contrib / scripts / safe_asterisk
1 #!/bin/sh
2 CLIARGS="$*"            # Grab any args passed to safe_asterisk
3 TTY=9                   # TTY (if you want one) for Asterisk to run on
4 CONSOLE=yes             # Whether or not you want a console
5 #NOTIFY=ben@alkaloid.net        # Who to notify about crashes
6 MACHINE=`hostname`      # To specify which machine has crashed when getting the mail
7 DUMPDROP=/tmp
8 ASTSBINDIR=__ASTERISK_SBIN_DIR__
9 #
10 # Don't fork when running "safely"
11 #
12 ASTARGS=""
13 if [ "$TTY" != "" ]; then
14         if [ -c /dev/tty${TTY} ]; then
15                 TTY=tty${TTY}
16         elif [ -c /dev/vc/${TTY} ]; then
17                 TTY=vc/${TTY}
18         else
19                 echo "Cannot find your TTY (${TTY})" >&2
20                 exit 1
21         fi
22         ASTARGS="${ASTARGS} -vvvg"
23         if [ "$CONSOLE" != "no" ]; then
24                 ASTARGS="${ASTARGS} -c"
25         fi
26 fi
27 if [ ! -w ${DUMPDROP} ]; then   
28         echo "Cannot write to ${DUMPDROP}" >&2
29         exit 1
30 fi
31
32 #
33 # Let Asterisk dump core
34 #
35 ulimit -c unlimited
36
37 #launch_asterisk()
38 #{
39 #}
40
41 run_asterisk()
42 {
43         while :; do 
44
45                 if [ "$TTY" != "" ]; then
46                         cd /tmp
47                         stty sane < /dev/${TTY}
48                         ${ASTSBINDIR}/asterisk ${CLIARGS} ${ASTARGS} >& /dev/${TTY} < /dev/${TTY}
49                 else
50                         cd /tmp
51                         ${ASTSBINDIR}/asterisk ${CLIARGS} ${ASTARGS}
52                 fi
53                 EXITSTATUS=$?
54                 echo "Asterisk ended with exit status $EXITSTATUS"
55                 if [ "$EXITSTATUS" = "0" ]; then
56                         # Properly shutdown....
57                         echo "Asterisk shutdown normally."
58                         exit 0
59                 elif [ $EXITSTATUS -gt 128 ]; then
60                         let EXITSIGNAL=EXITSTATUS-128
61                         echo "Asterisk exited on signal $EXITSIGNAL."
62                         if [ "$NOTIFY" != "" ]; then
63                                 echo "Asterisk on $MACHINE exited on signal $EXITSIGNAL.  Might want to take a peek." | \
64                                 mail -s "Asterisk Died" $NOTIFY
65                         fi
66                         if [ -f /tmp/core ]; then
67                                 mv /tmp/core ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
68                         fi
69                 else
70                         if [ "${EXITSTATUS}" = "0" ]; then
71                                 echo "Asterisk ended normally.  Aborting."
72                                 exit 0
73                         else
74                                 echo "Asterisk died with code $EXITSTATUS."
75                                 if [ -f /tmp/core ]; then
76                                         mv /tmp/core ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
77                                 fi
78                         fi
79                 fi
80                 echo "Automatically restarting Asterisk."
81                 sleep 4
82         done
83 }
84
85 run_asterisk &