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