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