Version 0.2.0 from FTP
[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=                 # Who to notify about crashes
5 #
6 # Don't fork when running "safely"
7 #
8 ASTARGS=""
9 if [ "$TTY" != "" ]; then
10         ASTARGS="${ASTARGS} -vvv"
11         if [ "$CONSOLE" != "no" ]; then
12                 ASTARGS="${ASTARGS} -c"
13         fi
14 fi
15
16 #
17 # Let Asterisk dump core
18 #
19 ulimit -c unlimited
20
21 launch_asterisk()
22 {
23 }
24
25 run_asterisk()
26 {
27         while :; do 
28
29                 if [ "$TTY" != "" ]; then
30                         stty sane < /dev/tty${TTY}
31                         asterisk ${ASTARGS} >& /dev/tty${TTY} < /dev/tty${TTY}
32                 else
33                         asterisk ${ASTARGS}
34                 fi
35                 EXITSTATUS=$?
36                 #echo "Asterisk ended with exit status $EXITSTATUS"
37                 if [ "$EXITSTATUS" = "0" ]; then
38                         # Properly shutdown....
39                         echo "Asterisk shutdown normally."
40                         exit 0
41                 elif [ $EXITSTATUS -gt 128 ]; then
42                         let EXITSIGNAL=EXITSTATUS-128
43                         echo "Asterisk exited on signal $EXITSIGNAL."
44                         if [ "$NOTIFY" != "" ]; then
45                                 echo "Asterisk exited on signal $EXITSIGNAL.  Might want to take a peek." | \
46                                 mail -s "Asterisk Died" $NOTIFY
47                         fi
48                 else
49                         echo "Asterisk died with code $EXITSTATUS.  Aborting."
50                         exit 0
51                 fi
52                 echo "Automatically restarting Asterisk."
53         done
54 }
55
56 run_asterisk &