Version 0.1.12 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="-f"
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 while :; do 
22
23         if [ "$TTY" != "" ]; then
24                 stty sane < /dev/tty${TTY}
25                 asterisk ${ASTARGS} >& /dev/tty${TTY} < /dev/tty${TTY}
26         else
27                 asterisk ${ASTARGS}
28         fi
29         EXITSTATUS=$?
30         #echo "Asterisk ended with exit status $EXITSTATUS"
31         if [ "$EXITSTATUS" = "0" ]; then
32                 # Properly shutdown....
33                 echo "Asterisk shutdown normally."
34                 exit 0
35         elif [ $EXITSTATUS -gt 128 ]; then
36                 let EXITSIGNAL=EXITSTATUS-128
37                 echo "Asterisk exited on signal $EXITSIGNAL."
38                 if [ "$NOTIFY" != "" ]; then
39                         echo "Asterisk exited on signal $EXITSIGNAL.  Might want to take a peek." | \
40                         mail -s "Asterisk Died" $NOTIFY
41                 fi
42         else
43                 echo "Asterisk died with code $EXITSTATUS.  Aborting."
44                 exit 0
45         fi
46         echo "Automatically restarting Asterisk."
47 done