df61e774634603d9893c90eea9f76d13719fb051
[asterisk/asterisk.git] / contrib / init.d / rc.redhat.asterisk
1 #!/bin/sh
2 # $Id$
3 #
4 # asterisk    Starts, Stops and Reloads Asterisk.
5 #
6 # chkconfig: 2345 90 60
7 # description: Asterisk PBX and telephony daemon.
8 # processname: asterisk
9 # pidfile: /var/run/asterisk.pid
10 #
11 # Thu Nov 17 2005 Gregory Boehnlein <damin@nacs.net>
12 # - Updated Version to 1.3
13 # - Reversed behavior of LD_ASSUME_KERNEL=2.4.1
14 # - Added detailed failure messages
15 #
16 # Sun Jul 18 2004 Gregory Boehnlein <damin@nacs.net>
17 # - Updated Version to 1.2
18 # - Added test for safe_asterisk
19 # - Verified SIGTERM issued by "killproc" ensures "stop gracefully"
20 # - Added support for -U and -G command line options
21 # - Modified "reload" to call asterisk -rx 'reload' 
22
23 # Use this option to specify a different configuration directory
24 #AST_CONFIG=/etc/asterisk
25
26 # Installation directory
27 AST_SBIN=/usr/sbin
28
29 # Source function library.
30 . /etc/rc.d/init.d/functions
31
32 if ! [ -x $AST_SBIN/asterisk ] ; then
33         echo "ERROR: $AST_SBIN/asterisk not found"
34         exit 0
35 fi
36
37 if ! [ -d $AST_CONFIG ] ; then
38         echo "ERROR: $AST_CONFIG directory not found"
39         exit 0
40 fi
41
42 # Full path to asterisk binary
43 DAEMON=$AST_SBIN/asterisk
44
45 # Full path to safe_asterisk script
46 SAFE_ASTERISK=$AST_SBIN/safe_asterisk
47
48 # Allow configuration overrides in /etc/sysconfig/asterisk
49 CONFIG0=`readlink $0`
50 if [ "$CONFIG0" = "" ]; then
51         CONFIGFILE=/etc/sysconfig/`basename $0`
52 else
53         CONFIGFILE=/etc/sysconfig/`basename $CONFIG0`
54 fi
55 [ -r $CONFIGFILE ] && . $CONFIGFILE
56
57 RETVAL=0
58
59 start() {
60         # Check if Asterisk is already running.  If it is, then bug out, because
61         # starting safe_asterisk when Asterisk is running is very bad.
62         VERSION=`${AST_SBIN}/asterisk -rx 'core show version'`
63         if [ "`echo $VERSION | cut -c 1-8`" = "Asterisk" ]; then
64                 echo "Asterisk is already running."
65                 exit 1
66         fi
67
68         # Start daemons.
69         echo -n $"Starting asterisk: "
70         if [ -f $SAFE_ASTERISK ] ; then
71                 DAEMON=$SAFE_ASTERISK
72         fi
73         if [ $AST_USER ] ; then
74                 ASTARGS="-U $AST_USER"
75         fi
76         if [ $AST_GROUP ] ; then
77                 ASTARGS="$ASTARGS -G $AST_GROUP"
78         fi
79         if [ $AST_CONFIG ]; then
80                 ASTARGS="$ASTARGS -C $AST_CONFIG/asterisk.conf"
81         elif [ $ALTCONF ]; then
82                 ASTARGS="$ASTARGS -C $ALTCONF"
83         fi
84         if [ "x$COREDUMP" = "xyes" ]; then
85                 ASTARGS="$ASTARGS -g"
86         fi
87         if [ "0$MAXLOAD" -gt "0" ]; then
88                 ASTARGS="$ASTARGS -L $MAXLOAD"
89         fi
90         if [ "0$MAXCALLS" -gt "0" ]; then
91                 ASTARGS="$ASTARGS -M $MAXCALLS"
92         fi
93         if [ "0$VERBOSITY" -gt "0" ]; then
94                 for i in `seq 1 $VERBOSITY`; do
95                         ASTARGS="$ASTARGS -v"
96                 done
97         fi
98         if [ "x$INTERNALTIMING" = "xyes" ]; then
99                 ASTARGS="$ASTARGS -I"
100         fi
101         if [ "x$TEMPRECORDINGLOCATION" = "xyes" -o "x$TMPRECORDINGLOCATION" = "xyes" ]; then
102                 ASTARGS="$ASTARGS -t"
103         fi
104         if [ "x$COLOR" = "xyes" ]; then
105                 export TERM=linux
106                 daemon sh -c "$DAEMON $ASTARGS -c" >/dev/null </dev/null 2>&1 &
107         else
108                 daemon $DAEMON $ASTARGS
109         fi
110         RETVAL=$?
111         [ $RETVAL -eq 0 ] && touch /var/lock/subsys/asterisk
112         echo
113         return $RETVAL
114 }
115
116 stop() {
117         # Stop daemons.
118         if [ -f $SAFE_ASTERISK ]; then
119                 # Kill safe_asterisk first, to prevent us from starting up 2 instances of safe_asterisk
120                 echo -n $"Stopping safe_asterisk: "
121                 killproc `basename $SAFE_ASTERISK`
122                 echo
123         fi
124         echo -n $"Shutting down asterisk: "
125         killproc `basename $DAEMON`
126         RETVAL=$?
127         [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/asterisk
128         echo
129         return $RETVAL
130 }
131
132 restart() {
133         stop
134         start
135 }
136
137 reload() {
138         $DAEMON -rx 'module reload' > /dev/null 2> /dev/null
139 }
140
141 # See how we were called.
142 case "$1" in
143   start)
144         start
145         ;;
146   stop)
147         stop
148         ;;
149   restart)
150         restart
151         ;;
152   reload)
153         reload
154         ;;
155   condrestart)
156         [ -f /var/lock/subsys/asterisk ] && restart || :
157         ;;
158   status)
159         status asterisk
160         ;;
161   *)
162         echo "Usage: asterisk {start|stop|restart|reload|condrestart|status}"
163         exit 1
164 esac
165
166 exit $?