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