Make redhat init.d script call asterisk to reload instead of forcing a restart (bug...
[asterisk/asterisk.git] / contrib / init.d / rc.mandrake.asterisk
1 #!/bin/sh
2
3 # asterisk:             Starts the asterisk service
4
5 # Version:              @(#) /etc/rc.d/init.d/asterisk 1.0
6
7 # chkconfig: 2345 95 10
8 # description:  Starts the asterisk service
9
10 # processname:  asterisk
11
12
13 TTY=9                   # TTY (if you want one) for Asterisk to run on
14 CONSOLE=yes             # Whether or not you want a console
15 NOTIFY=root             # Who to notify about crashes
16 DUMPDROP=/tmp
17 HOSTNAME=`hostname`
18
19 # Setup environment
20 cd /usr/src
21 OPENH323DIR=/usr/src/openh323
22 PWLIBDIR=/usr/src/pwlib
23 LD_LIBRARY_PATH=$OPENH323DIR/lib:$PWLIBDIR/lib
24 export OPENH323DIR PWLIBDIR LD_LIBRARY_PATH
25
26 # Source function library.
27 . /etc/rc.d/init.d/functions
28
29 #
30 # Don't fork when running "safely"
31 #
32 ASTARGS="-p"
33 if [ "$TTY" != "" ]; then
34         if [ -c /dev/tty${TTY} ]; then
35                 TTY=tty${TTY}
36         elif [ -c /dev/vc/${TTY} ]; then
37                 TTY=vc/${TTY}
38         else
39                 echo "Cannot find your TTY (${TTY})" >&2
40                 exit 1
41         fi
42         ASTARGS="${ASTARGS} -vvv"
43         if [ "$CONSOLE" != "no" ]; then
44                 ASTARGS="${ASTARGS} -c"
45         fi
46 fi
47 if [ ! -w ${DUMPDROP} ]; then   
48         echo "Cannot write to ${DUMPDROP}" >&2
49         exit 1
50 fi
51
52 #
53 # Let Asterisk dump core
54 #
55 ulimit -c unlimited
56
57 #launch_asterisk()
58 #{
59 #}
60
61 run_asterisk()
62 {
63         while :; do 
64
65                 if [ "$TTY" != "" ]; then
66                         cd /tmp
67                         stty sane < /dev/${TTY}
68                         asterisk ${ASTARGS} > /dev/${TTY} 2>&1 < /dev/${TTY}
69                 else
70                         cd /tmp
71                         asterisk ${ASTARGS}
72                 fi
73                 EXITSTATUS=$?
74                 echo "Asterisk ended with exit status $EXITSTATUS"
75                 if [ "$EXITSTATUS" = "0" ]; then
76                         # Properly shutdown....
77                         echo "Asterisk shutdown normally."
78                         exit 0
79                 elif [ $EXITSTATUS -gt 128 ]; then
80                         if [ "$EXITSTATUS" = "129" ]; then
81                                 EXITSIGNAL=1
82                                 EXITMSG="Hangup"
83                         elif [ "$EXITSTATUS" = "130" ]; then
84                                 EXITSIGNAL=2
85                                 EXITMSG="Interrupt"
86                         elif [ "$EXITSTATUS" = "131" ]; then
87                                 EXITSIGNAL=3
88                                 EXITMSG="Quit"
89                         elif [ "$EXITSTATUS" = "132" ]; then
90                                 EXITSIGNAL=4
91                                 EXITMSG="Illegal instruction"
92                         elif [ "$EXITSTATUS" = "133" ]; then
93                                 EXITSIGNAL=5
94                                 EXITMSG="Trace trap"
95                         elif [ "$EXITSTATUS" = "134" ]; then
96                                 EXITSIGNAL=6
97                                 EXITMSG="IOT Trap"
98                         elif [ "$EXITSTATUS" = "135" ]; then
99                                 EXITSIGNAL=7
100                                 EXITMSG="Bus Error"
101                         elif [ "$EXITSTATUS" = "136" ]; then
102                                 EXITSIGNAL=8
103                                 EXITMSG="Floating-point exception"
104                         elif [ "$EXITSTATUS" = "137" ]; then
105                                 EXITSIGNAL=9
106                                 EXITMSG="Killed"
107                         elif [ "$EXITSTATUS" = "138" ]; then
108                                 EXITSIGNAL=10
109                                 EXITMSG="User-defined signal 1"
110                         elif [ "$EXITSTATUS" = "139" ]; then
111                                 EXITSIGNAL=11
112                                 EXITMSG="Segmentation violation"
113                         elif [ "$EXITSTATUS" = "140" ]; then
114                                 EXITSIGNAL=12
115                                 EXITMSG="User-defined signal 2"
116                         elif [ "$EXITSTATUS" = "141" ]; then
117                                 EXITSIGNAL=13
118                                 EXITMSG="Broken pipe"
119                         elif [ "$EXITSTATUS" = "142" ]; then
120                                 EXITSIGNAL=14
121                                 EXITMSG="Alarm clock"
122                         elif [ "$EXITSTATUS" = "143" ]; then
123                                 EXITSIGNAL=15
124                                 EXITMSG="Termination"
125                         elif [ "$EXITSTATUS" = "144" ]; then
126                                 EXITSIGNAL=16
127                                 EXITMSG="Stack fault"
128                         fi
129                         echo "Asterisk exited on signal $EXITSIGNAL - $EXITMSG."
130                         if [ "$NOTIFY" != "" ]; then
131                                 echo "Asterisk exited on signal $EXITSIGNAL - $EXITMSG.  Might want to take a peek." | \
132                                 mail -s "Asterisk Died ($HOSTNAME)" $NOTIFY
133                         fi
134                         if [ -f /tmp/core ]; then
135                                 mv /tmp/core ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
136                         fi
137                 else
138                         echo "Asterisk died with code $EXITSTATUS.  Aborting."
139                         if [ -f /tmp/core ]; then
140                                 mv /tmp/core ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
141                         fi
142                         exit 0
143                 fi
144                 echo "Automatically restarting Asterisk."
145         done
146 }
147
148 case "$1" in
149         start)
150                 gprintf "Starting asterisk: "
151                 run_asterisk >/dev/null 2>&1 &
152                 sleep 2 # Give it time to die
153                 succeeded=`pidof asterisk|awk '{print NF}'`
154                 if [ $succeeded = "0" ]; then
155                         failure
156                 else
157                         success
158                 fi
159                 echo
160                 ;;
161         stop)
162                 gprintf "Stopping asterisk: "
163                 asterisk -r -x "stop gracefully" >/dev/null 2>&1
164                 killall -9 mpg123 2>/dev/null
165                 success
166                 echo
167                 ;;
168         restart)
169                 $0 stop
170                 usleep 100000
171                 $0 start
172                 ;;
173         reload)
174                 gprintf "Reloading asterisk: "
175                 asterisk -r -x "reload" >/dev/null 2>&1
176                 success
177                 echo
178                 ;;
179         stopnow)
180                 gprintf "Stopping asterisk: "
181                 asterisk -r -x "stop now" >/dev/null 2>&1
182                 success
183                 echo
184                 ;;
185         restartnow)
186                 $0 stopnow
187                 $0 start
188                 ;;
189         status)
190                 succeeded=`pidof asterisk|awk '{print NF}'`
191                 if [ $succeeded = "0" ]; then
192                         echo "Asterisk is not running"
193                 else
194                         echo "Asterisk is currently running with $succeeded threads"
195                 fi
196                 ;;
197         *)
198                 gprintf "*** Usage: $0 {start|stop|reload|restart|stopnow|restartnow|status}\n"
199                 exit 1
200 esac
201
202 exit 0
203