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