Merged revisions 115285 via svnmerge from
[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                 # Check if Asterisk is already running.  If it is, then bug out, because
123                 # starting Asterisk when Asterisk is already running is very bad.
124                 VERSION=`${ASTSBINDIR}/asterisk -rx 'core show version'`
125                 if [ "${VERSION:0:8}" = "Asterisk" ]; then # otherwise "Unable t"
126                         echo "Asterisk is already running.  $0 will exit now."
127                         exit 1
128                 fi
129
130                 gprintf "Starting asterisk: "
131                 run_asterisk >/dev/null 2>&1 &
132                 sleep 2 # Give it time to die
133                 succeeded=`pidof asterisk|awk '{print NF}'`
134                 if [ $succeeded = "0" ]; then
135                         failure
136                 else
137                         success
138                 fi
139                 echo
140                 ;;
141         stop)
142                 gprintf "Stopping asterisk: "
143                 asterisk -r -x "stop gracefully" >/dev/null 2>&1
144                 killall -9 mpg123 2>/dev/null
145                 success
146                 echo
147                 ;;
148         restart)
149                 $0 stop
150                 usleep 100000
151                 $0 start
152                 ;;
153         reload)
154                 gprintf "Reloading asterisk: "
155                 asterisk -r -x "reload" >/dev/null 2>&1
156                 success
157                 echo
158                 ;;
159         stopnow)
160                 gprintf "Stopping asterisk: "
161                 asterisk -r -x "stop now" >/dev/null 2>&1
162                 success
163                 echo
164                 ;;
165         restartnow)
166                 $0 stopnow
167                 $0 start
168                 ;;
169         fullrestart)
170                 $0 stop
171                 service zaptel restart
172                 $0 start
173                 ;;
174         fullrestartnow)
175                 $0 stopnow
176                 service zaptel restart
177                 $0 start
178                 ;;
179         status)
180                 succeeded=`pidof asterisk|awk '{print NF}'`
181                 if [ $succeeded = "0" ]; then
182                         echo "Asterisk is not running"
183                 else
184                         echo "Asterisk is currently running with $succeeded threads"
185                 fi
186                 ;;
187         *)
188                 gprintf "*** Usage: $0 {start|stop[now]|reload|[full]restart[now]|status}\n"
189                 exit 1
190 esac
191
192 exit 0
193