5452fdda781905b1c1dbaf873a49717f60aa707d
[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 CONFIGFILE=/etc/sysconfig/`basename $0`
19
20 # Setup environment
21 cd /usr/src
22 if [ -f /usr/lib/asterisk/modules/chan_h323.so -a `grep -c ^noload=chan_h323.so /etc/asterisk/modules.conf` -eq 0 ]; then
23         OPENH323DIR=/usr/src/h323/openh323
24         PWLIBDIR=/usr/src/h323/pwlib
25 else
26         OPENH323DIR=/usr/src/oh323/openh323
27         PWLIBDIR=/usr/src/oh323/pwlib
28 fi
29
30 # Put overrides in /etc/sysconfig/asterisk
31 [ -f $CONFIGFILE ] && . $CONFIGFILE
32
33 LD_LIBRARY_PATH=$OPENH323DIR/lib:$PWLIBDIR/lib
34 export OPENH323DIR PWLIBDIR LD_LIBRARY_PATH
35
36 # Source function library.
37 . /etc/rc.d/init.d/functions
38
39 #
40 # Don't fork when running "safely"
41 #
42 ASTARGS="-p"
43 if [ "$TTY" != "" ]; then
44         if [ -c /dev/tty${TTY} ]; then
45                 TTY=tty${TTY}
46         elif [ -c /dev/vc/${TTY} ]; then
47                 TTY=vc/${TTY}
48         else
49                 echo "Cannot find your TTY (${TTY})" >&2
50                 exit 1
51         fi
52         ASTARGS="${ASTARGS} -vvv"
53         if [ "$CONSOLE" != "no" ]; then
54                 ASTARGS="${ASTARGS} -c"
55         fi
56 fi
57 if [ ! -w ${DUMPDROP} ]; then   
58         echo "Cannot write to ${DUMPDROP}" >&2
59         exit 1
60 fi
61
62 #
63 # Let Asterisk dump core
64 #
65 ulimit -c unlimited
66
67 #launch_asterisk()
68 #{
69 #}
70
71 SIGMSG=("Hangup" "Interrupt" "Quit" "Illegal instruction" "Trace trap" "IOT Trap" "Bus Error" "Floating-point exception" "Killed" "User-defined signal 1" "Segmentation violation" "User-defined signal 2" "Broken pipe" "Alarm clock" "Termination" "Stack fault")
72
73 run_asterisk()
74 {
75         while :; do 
76
77                 if [ "$TTY" != "" ]; then
78                         cd /tmp
79                         stty sane < /dev/${TTY}
80                         asterisk ${ASTARGS} > /dev/${TTY} 2>&1 < /dev/${TTY}
81                 else
82                         cd /tmp
83                         asterisk ${ASTARGS}
84                 fi
85                 EXITSTATUS=$?
86                 echo "Asterisk ended with exit status $EXITSTATUS"
87                 if [ "$EXITSTATUS" = "0" ]; then
88                         # Properly shutdown....
89                         echo "Asterisk shutdown normally."
90                         exit 0
91                 elif [ $EXITSTATUS -gt 128 ]; then
92                         EXITSIGNAL=$(($EXITSTATUS - 128))
93                         EXITMSG=$SIGMSG[$EXITSIGNAL]
94                         echo "Asterisk exited on signal $EXITSIGNAL - $EXITMSG."
95                         if [ "$NOTIFY" != "" ]; then
96                                 echo "Asterisk exited on signal $EXITSIGNAL - $EXITMSG.  Might want to take a peek." | \
97                                 mail -s "Asterisk Died ($HOSTNAME)" $NOTIFY
98                         fi
99                         if [ -f /tmp/core ]; then
100                                 mv /tmp/core ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
101                         fi
102                 else
103                         echo "Asterisk died with code $EXITSTATUS.  Aborting."
104                         if [ -f /tmp/core ]; then
105                                 mv /tmp/core ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
106                         fi
107                         exit 0
108                 fi
109                 echo "Automatically restarting Asterisk."
110         done
111 }
112
113 case "$1" in
114         start)
115                 gprintf "Starting asterisk: "
116                 run_asterisk >/dev/null 2>&1 &
117                 sleep 2 # Give it time to die
118                 succeeded=`pidof asterisk|awk '{print NF}'`
119                 if [ $succeeded = "0" ]; then
120                         failure
121                 else
122                         success
123                 fi
124                 echo
125                 ;;
126         stop)
127                 gprintf "Stopping asterisk: "
128                 asterisk -r -x "stop gracefully" >/dev/null 2>&1
129                 killall -9 mpg123 2>/dev/null
130                 success
131                 echo
132                 ;;
133         restart)
134                 $0 stop
135                 usleep 100000
136                 $0 start
137                 ;;
138         reload)
139                 gprintf "Reloading asterisk: "
140                 asterisk -r -x "reload" >/dev/null 2>&1
141                 success
142                 echo
143                 ;;
144         stopnow)
145                 gprintf "Stopping asterisk: "
146                 asterisk -r -x "stop now" >/dev/null 2>&1
147                 success
148                 echo
149                 ;;
150         restartnow)
151                 $0 stopnow
152                 $0 start
153                 ;;
154         fullrestart)
155                 $0 stop
156                 service zaptel restart
157                 $0 start
158                 ;;
159         fullrestartnow)
160                 $0 stopnow
161                 service zaptel restart
162                 $0 start
163                 ;;
164         status)
165                 succeeded=`pidof asterisk|awk '{print NF}'`
166                 if [ $succeeded = "0" ]; then
167                         echo "Asterisk is not running"
168                 else
169                         echo "Asterisk is currently running with $succeeded threads"
170                 fi
171                 ;;
172         *)
173                 gprintf "*** Usage: $0 {start|stop[now]|reload|[full]restart[now]|status}\n"
174                 exit 1
175 esac
176
177 exit 0
178