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