update doxygen configuration file to current version (bug #4144)
[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 #
9 # Don't fork when running "safely"
10 #
11 ASTARGS=""
12 if [ "$TTY" != "" ]; then
13         if [ -c /dev/tty${TTY} ]; then
14                 TTY=tty${TTY}
15         elif [ -c /dev/vc/${TTY} ]; then
16                 TTY=vc/${TTY}
17         else
18                 echo "Cannot find your TTY (${TTY})" >&2
19                 exit 1
20         fi
21         ASTARGS="${ASTARGS} -vvvg"
22         if [ "$CONSOLE" != "no" ]; then
23                 ASTARGS="${ASTARGS} -c"
24         fi
25 fi
26 if [ ! -w ${DUMPDROP} ]; then   
27         echo "Cannot write to ${DUMPDROP}" >&2
28         exit 1
29 fi
30
31 #
32 # Let Asterisk dump core
33 #
34 ulimit -c unlimited
35
36 #launch_asterisk()
37 #{
38 #}
39
40 run_asterisk()
41 {
42         while :; do 
43
44                 if [ "$TTY" != "" ]; then
45                         cd /tmp
46                         stty sane < /dev/${TTY}
47                         asterisk ${CLIARGS} ${ASTARGS} >& /dev/${TTY} < /dev/${TTY}
48                 else
49                         cd /tmp
50                         asterisk ${CLIARGS} ${ASTARGS}
51                 fi
52                 EXITSTATUS=$?
53                 echo "Asterisk ended with exit status $EXITSTATUS"
54                 if [ "$EXITSTATUS" = "0" ]; then
55                         # Properly shutdown....
56                         echo "Asterisk shutdown normally."
57                         exit 0
58                 elif [ $EXITSTATUS -gt 128 ]; then
59                         let EXITSIGNAL=EXITSTATUS-128
60                         echo "Asterisk exited on signal $EXITSIGNAL."
61                         if [ "$NOTIFY" != "" ]; then
62                                 echo "Asterisk on $MACHINE exited on signal $EXITSIGNAL.  Might want to take a peek." | \
63                                 mail -s "Asterisk Died" $NOTIFY
64                         fi
65                         if [ -f /tmp/core ]; then
66                                 mv /tmp/core ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
67                         fi
68                 else
69                         if [ "${EXITSTATUS}" = "0" ]; then
70                                 echo "Asterisk ended normally.  Aborting."
71                                 exit 0
72                         else
73                                 echo "Asterisk died with code $EXITSTATUS."
74                                 if [ -f /tmp/core ]; then
75                                         mv /tmp/core ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
76                                 fi
77                         fi
78                 fi
79                 echo "Automatically restarting Asterisk."
80                 sleep 4
81         done
82 }
83
84 run_asterisk &