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